forked from darpanjbora/Java-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicNumber.java
More file actions
43 lines (35 loc) · 800 Bytes
/
Copy pathMagicNumber.java
File metadata and controls
43 lines (35 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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