Find nth Magic Number | Java program to find nth magic number ; Function to find nth magic number ; Go through every bit of n ; If last bit of n is set ; proceed to next bit or n = n / 2 ; Driver program to test above function
import java . io . * ; class GFG { static int nthMagicNo ( int n ) { int pow = 1 , answer = 0 ; while ( n != 0 ) { pow = pow * 5 ; if ( ( int ) ( n & 1 ) == 1 ) answer += pow ; n >>= 1 ; } return answer ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " nth ▁ magic " + " ▁ number ▁ is ▁ " + nthMagicNo ( n ) ) ; } }