Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2fc598

Browse files
committed
Added PowerOfTwoOrNot to Others
1 parent2ae4930 commitb2fc598

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎Others/PowerOfTwoOrNot.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
importjava.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+
publicclassPowerOfTwoOrNot {
8+
9+
publicstaticvoidmain (String[]args) {
10+
11+
Scannersc =newScanner(System.in);
12+
System.out.println("Enter the number");
13+
intnum =sc.nextInt();
14+
booleanisPowerOfTwo =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+
publicstaticbooleancheckIfPowerOfTwoOrNot(intnumber) {
30+
returnnumber !=0 && ((number & (number-1)) ==0);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp