File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ /**
4
+ *A utility to check if a given number is power of two or not.
5
+ *For example 8,16 etc.
6
+ */
7
+ public class PowerOfTwoOrNot {
8
+
9
+ public static void main (String [] args ) {
10
+
11
+ Scanner sc = new Scanner (System .in );
12
+ System .out .println ("Enter the number" );
13
+ int num = sc .nextInt ();
14
+ boolean isPowerOfTwo = checkIfPowerOfTwoOrNot (num );
15
+ if (isPowerOfTwo ) {
16
+ System .out .println ("Number is a power of two" );
17
+ } else {
18
+ System .out .println ("Number is not a power of two" );
19
+ }
20
+ }
21
+
22
+
23
+ /**
24
+ * Checks whether given number is power of two or not.
25
+ *
26
+ * @param number
27
+ * @return boolean
28
+ */
29
+ public static boolean checkIfPowerOfTwoOrNot (int number ) {
30
+ return number != 0 && ((number & (number -1 )) == 0 );
31
+ }
32
+
33
+ }
You can’t perform that action at this time.
0 commit comments