Find the greater number closest to N having at most one non | 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
function power ( X , n ) { var res = 1 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) res = res * X ; X = X * X ; n = n >> 1 ; } return res ; } function closestgtNum ( N ) { var n = parseInt ( Math . log10 ( N ) + 1 ) ; var P = power ( 10 , n - 1 ) ; var Y = N % P ; var res = N + ( P - Y ) ; return res ; } var N = 120 ; document . write ( closestgtNum ( N ) ) ;