Smallest power of 2 consisting of N digits | C ++ Program to implement the above approach ; Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestNum ( int n ) { int res = 1 ; for ( int i = 2 ; ; i *= 2 ) { int length = log10 ( i ) + 1 ; if ( length == n ) return log ( i ) / log ( 2 ) ; } } int main ( ) { int n = 4 ; cout << smallestNum ( n ) ; return 0 ; }