Find the greater number closest to N having at most one non | C ++ program to implement the above approach ; Function to get closest greater number with at most non zero digit ; Stores the closest greater number with at most one non - zero digit ; Stores length of str ; Append 10 to the end of resultant string ; Append n - 1 times '0' to the end of resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string closestgtNum ( string str ) { string res = " " ; int n = str . length ( ) ; if ( str [ 0 ] < '9' ) { res . push_back ( str [ 0 ] + 1 ) ; } else { res . push_back ( '1' ) ; res . push_back ( '0' ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { res . push_back ( '0' ) ; } return res ; } int main ( ) { string str = "120" ; cout << closestgtNum ( str ) ; }