Smallest and Largest N | Python3 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 math NEW_LINE def findNumberL ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return "1" NEW_LINE DEDENT result = " " NEW_LINE length = math . floor ( math . log10 ( n ) + 1 ) NEW_LINE for i in range ( 1 , n - ( 2 * length ) + 1 ) : NEW_LINE INDENT result += '9' NEW_LINE DEDENT result = ( str ( n ) + result + str ( n ) ) NEW_LINE return result NEW_LINE DEDENT def findNumberS ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return "1" NEW_LINE DEDENT result = " " NEW_LINE length = math . floor ( math . log10 ( n ) + 1 ) NEW_LINE for i in range ( 1 , n - ( 2 * length ) + 1 ) : NEW_LINE INDENT result += '0' NEW_LINE DEDENT result = ( str ( n ) + result + str ( n ) ) NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE print ( " Smallest ▁ Number ▁ = ▁ " + findNumberS ( N ) ) NEW_LINE print ( " Largest ▁ Number ▁ = ▁ " + findNumberL ( N ) ) NEW_LINE DEDENT