-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ_power_of_two.java
More file actions
61 lines (55 loc) · 1.9 KB
/
Copy pathJ_power_of_two.java
File metadata and controls
61 lines (55 loc) · 1.9 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package Recursion;
/* Task 10 */
/*
For the following task:
• Any other libraries except very basic ones are NOT allowed.
• Global and Static variables are not allowed.
• You can’t add more parameters or call helper functions.
Power of two. Given 1 parameter - 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 = 2^x
Test the function properly in main and give the output as shown in Figure 1.
Figure 1:
0 is not a power of two
1 is a power of two
2 is a power of two
3 is not a power of two
4 is a power of two
5 is not a power of two
6 is not a power of two
7 is not a power of two
8 is a power of two
9 is not a power of two
10 is not a power of two
12 is not a power of two
14 is not a power of two
16 is a power of two
18 is not a power of two
20 is not a power of two
22 is not a power of two
24 is not a power of two
26 is not a power of two
28 is not a power of two
30 is not a power of two
32 is a power of two
*/
public class J_power_of_two {
public static void main(String[] args) {
for (int i = 0; i < 32; i++) {
if (i > 10) i++;
if(isPowerOfTwo(i)) System.out.println(i + " is a power of two");
else System.out.println(i + " is not a power of two");
}
}
private static Boolean isPowerOfTwo(int n){
if (n <= 0) {
return false;
} else if (n == 1) {
return true;
} else if (n % 2 != 0) {
return false;
} else {
return isPowerOfTwo(n / 2); // Return the result of recursive call
}
}
}