Smallest and Largest N | C ++ program for the above approach ; Function to find n digit largest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit largest number ; Find the number of digits in number n ; Append 9 ; To make it largest n digit number starting and ending with n , we just need to append n at start and end ; Return the largest number ; Function to find n digit smallest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit smallest number ; Find the number of digits in number n ; To make it smallest n digit number starting and ending with n , we just need to append n at start and end ; Return the smallest number ; Driver code ; Given number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findNumberL ( int n ) { if ( n == 1 ) return "1" ; string result = " " ; int length = ( int ) floor ( log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '9' ; } result = to_string ( n ) + result + to_string ( n ) ; return result ; } string findNumberS ( int n ) { if ( n == 1 ) return "1" ; string result = " " ; int length = ( int ) floor ( log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '0' ; } result = to_string ( n ) + result + to_string ( n ) ; return result ; } int main ( ) { int N = 3 ; cout << " Smallest ▁ Number ▁ = ▁ " << findNumberS ( N ) << endl ; cout << " Largest ▁ Number ▁ = ▁ " << findNumberL ( N ) ; return 0 ; }