diff --git a/BitManipulation/Nth_magic_number/README.md b/BitManipulation/Nth_magic_number/README.md new file mode 100644 index 00000000..ccf566ba --- /dev/null +++ b/BitManipulation/Nth_magic_number/README.md @@ -0,0 +1,3 @@ +# MAGIC NUMBER + +A magic number is defined as a number which can be expressed as a power of 5 or sum of unique powers of 5. First few magic numbers are 5, 25, 30(5 + 25), 125, 130(125 + 5), …. \ No newline at end of file diff --git a/BitManipulation/Nth_magic_number/nth_magic_number.cpp b/BitManipulation/Nth_magic_number/nth_magic_number.cpp new file mode 100644 index 00000000..a3cc2648 --- /dev/null +++ b/BitManipulation/Nth_magic_number/nth_magic_number.cpp @@ -0,0 +1,25 @@ +#include +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; + cin>>n; + cout << "nth magic number is " << nthMagicNo(n) << endl; + return 0; +} \ No newline at end of file