Skip to content

Commit 979324d

Browse files
Create MagicNumber.java (loveBabbar#73)
* Create MagicNumber.java * Update and rename Lecture 5: Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/MagicNumber.java to Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/MagicNumber.java Co-authored-by: Pranay Gupta <[email protected]>
1 parent b680555 commit 979324d

File tree

1 file changed

+17
-0
lines changed
  • Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping

1 file changed

+17
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class MagicNumber {
2+
// Find the nth Magic Number
3+
public static void main(String [] args){
4+
int n = 9;
5+
int ans = 0;
6+
int base = 5;
7+
while(n > 0){
8+
// Find Last Digit of Binary number by and with 1
9+
int lastDigit = n & 1;
10+
ans = lastDigit * base;
11+
base = base * 5;
12+
n = n >> 1;
13+
}
14+
15+
System.out.println(" Ans is: " + ans);
16+
}
17+
}

0 commit comments

Comments
 (0)