mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 13:34:54 +08:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@ -14,7 +14,7 @@ public class CountChar {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter your text: ");
|
||||
String str = input.nextLine();
|
||||
|
||||
input.close();
|
||||
System.out.println("There are " + CountCharacters(str) + " characters.");
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ public class CountChar {
|
||||
* @return int: Number of characters in the passed string
|
||||
* */
|
||||
|
||||
public static int CountCharacters(String str) {
|
||||
private static int CountCharacters(String str) {
|
||||
|
||||
int count = 0;
|
||||
|
||||
|
33
Others/PowerOfTwoOrNot.java
Normal file
33
Others/PowerOfTwoOrNot.java
Normal file
@ -0,0 +1,33 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*A utility to check if a given number is power of two or not.
|
||||
*For example 8,16 etc.
|
||||
*/
|
||||
public class PowerOfTwoOrNot {
|
||||
|
||||
public static void main (String[] args) {
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter the number");
|
||||
int num = sc.nextInt();
|
||||
boolean isPowerOfTwo = checkIfPowerOfTwoOrNot(num);
|
||||
if (isPowerOfTwo) {
|
||||
System.out.println("Number is a power of two");
|
||||
} else {
|
||||
System.out.println("Number is not a power of two");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether given number is power of two or not.
|
||||
*
|
||||
* @param number
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean checkIfPowerOfTwoOrNot(int number) {
|
||||
return number != 0 && ((number & (number-1)) == 0);
|
||||
}
|
||||
|
||||
}
|
@ -18,8 +18,8 @@ import java.util.Scanner;
|
||||
input.close();
|
||||
}
|
||||
|
||||
public static int wordCount(String s){
|
||||
if(s.isEmpty() || s == null) return -1;
|
||||
private static int wordCount(String s){
|
||||
if(s.isEmpty() || s == null) return 0;
|
||||
return s.trim().split("[\\s]+").length;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user