Find smallest number formed by inverting digits of given number N | Java program for the above approach ; Function to invert the digits of integer N to form minimum possible number ; Initialize the array ; Iterate till the number N exists ; Last digit of the number N ; Checking if the digit is smaller than 9 - digit ; Store the smaller digit in the array ; Reduce the number each time ; Check if the digit starts with 0 or not ; Print the answer ; Driver Code ; Given Number ; Function Call
class GFG { static void number ( int num ) { int a [ ] = new int [ 20 ] ; int r , i = 0 , j ; while ( num > 0 ) { r = num % 10 ; if ( 9 - r > r ) a [ i ] = r ; else a [ i ] = 9 - r ; i ++ ; num = num / 10 ; } if ( a [ i - 1 ] == 0 ) { System . out . print ( "9" ) ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) System . out . print ( a [ j ] ) ; } public static void main ( String [ ] args ) { int num = 4545 ; number ( num ) ; } }