Find nth Magic Number | C ++ 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 n >>= 1 ; or n = n / 2 ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthMagicNo ( int n ) { int pow = 1 , answer = 0 ; while ( n ) { pow = pow * 5 ; if ( n & 1 ) answer += pow ; } return answer ; } int main ( ) { int n = 5 ; cout << " nth ▁ magic ▁ number ▁ is ▁ " << nthMagicNo ( n ) << endl ; return 0 ; }