Skip to content

Commit

Permalink
Merge pull request #54 from Mownika25/add-code
Browse files Browse the repository at this point in the history
algorithm for magic no.
  • Loading branch information
darpanjbora authored Oct 19, 2019
2 parents 569106e + 199a524 commit 51d64c2
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Miscellaneous/MagicNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Java program to find nth
// magic numebr
import java.io.*;

class MagicNumber
{
// Function to find nth magic number
static int nthMagicNo(int n)
{
int pow = 1, answer = 0;

// Go through every bit of n
while (n != 0)
{
pow = pow*5;

// If last bit of n is set
if ((int)(n & 1) == 1)
answer += pow;

// proceed to next bit
// or n = n/2
n >>= 1;
}
return answer;
}

// Driver program to test
// above function
public static void main(String[] args)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a no.");
int n = Integer.parseInt(br.readLine());

System.out.println(n+"th magic" +
" number is " + nthMagicNo(n));
}
}


// This code is contributed by
// prerna saini

0 comments on commit 51d64c2

Please sign in to comment.