Smallest power of 2 consisting of N digits | Java program to implement the above approach ; Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code
import java . io . * ; class GFG { static int smallestNum ( int n ) { int res = 1 ; for ( int i = 2 ; ; i *= 2 ) { int length = ( int ) ( Math . log10 ( i ) ) + 1 ; if ( length == n ) return ( int ) ( Math . log ( i ) / Math . log ( 2 ) ) ; } } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( smallestNum ( n ) ) ; } }