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