PowerofTwo.java
· 494 B · Java
Raw
import java.util.*;
import java.io.*;
class Main {
public static String PowersofTwo(int num) {
if (num > 0 && (num & (num - 1)) == 0) {
return "true";
} else {
return "false";
}
}
public static void main (String[] args) {
System.out.println(PowersofTwo(4)); // Output: true
System.out.println(PowersofTwo(124)); // Output: false
System.out.println(PowersofTwo(16)); // Output: true
System.out.println(PowersofTwo(22)); // Output: false
}
}
1 | import java.util.*; |
2 | import java.io.*; |
3 | |
4 | class Main { |
5 | public static String PowersofTwo(int num) { |
6 | if (num > 0 && (num & (num - 1)) == 0) { |
7 | return "true"; |
8 | } else { |
9 | return "false"; |
10 | } |
11 | } |
12 | |
13 | public static void main (String[] args) { |
14 | System.out.println(PowersofTwo(4)); // Output: true |
15 | System.out.println(PowersofTwo(124)); // Output: false |
16 | System.out.println(PowersofTwo(16)); // Output: true |
17 | System.out.println(PowersofTwo(22)); // Output: false |
18 | } |
19 | } |
20 |