Find smallest number formed by inverting digits of given number N | C ++ 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
#include <iostream> NEW_LINE using namespace std ; void number ( int num ) { int a [ 20 ] , 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 ) { cout << 9 ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) cout << a [ j ] ; } int main ( ) { long long int num = 4545 ; number ( num ) ; return 0 ; }