-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOfFour.java
More file actions
32 lines (23 loc) · 787 Bytes
/
PowerOfFour.java
File metadata and controls
32 lines (23 loc) · 787 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
// Write a Java program to check whether a given integer is a power of 4 or not. Given num = 64, return true. Given num = 6, return false.
import java.util.Scanner;
public class PowerOfFour {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a binary number: ");
int num = sc.nextInt();
boolean result = isPowerOfFour(num);
System.out.println(result);
}
public static boolean isPowerOfFour(int num) {
if (num <= 0) {
return false;
}
while (num > 1) {
if (num % 4 != 0) {
return false;
}
num = num / 4;
}
return true;
}
}