-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOfTwo.java
More file actions
36 lines (34 loc) · 837 Bytes
/
PowerOfTwo.java
File metadata and controls
36 lines (34 loc) · 837 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
/*
Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n == 2x.
Example 1:
Input: n = 1
Output: true
Explanation: 20 = 1
*/
public class PowerOfTwo {
public static void main(String[] args) {
int n=14;
boolean ans=isPowerOfTwo(n);
System.out.println(ans);
}
public static boolean isPowerOfTwo(int n) {
if(n==0){
return false;
}
int num=n;
boolean ans=true;
while (num>0){
if(num==1){
return true;
}
if (num%2==0){
num=num/2;
}
else {
return false;
}
}
return false;
}
}