Smallest and Largest N | Java 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
import java . io . * ; class GFG { static String findNumberL ( int n ) { if ( n == 1 ) return "1" ; String result = " " ; int length = ( int ) Math . floor ( Math . log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '9' ; } result = Integer . toString ( n ) + result + Integer . toString ( n ) ; return result ; } static String findNumberS ( int n ) { if ( n == 1 ) return "1" ; String result = " " ; int length = ( int ) Math . floor ( Math . log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '0' ; } result = Integer . toString ( n ) + result + Integer . toString ( n ) ; return result ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( " Smallest ▁ Number ▁ = ▁ " + findNumberS ( N ) ) ; System . out . print ( " Largest ▁ Number ▁ = ▁ " + findNumberL ( N ) ) ; } }