Find the greater number closest to N having at most one non | C ++ program to implement the above approach ; Function to calculate X ^ n in log ( n ) ; Stores the value of X ^ n ; If N is odd ; Function to find the closest number > N having at most 1 non - zero digit ; Stores the count of digits in N ; Stores the power of 10 ^ ( n - 1 ) ; Stores the last ( n - 1 ) digits ; Store the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int X , int n ) { int res = 1 ; while ( n ) { if ( n & 1 ) res = res * X ; X = X * X ; n = n >> 1 ; } return res ; } int closestgtNum ( int N ) { int n = log10 ( N ) + 1 ; int P = power ( 10 , n - 1 ) ; int Y = N % P ; int res = N + ( P - Y ) ; return res ; } int main ( ) { int N = 120 ; cout << closestgtNum ( N ) ; }