Write a Java program that reads a positive integer and counts the number of digits the number has.
e.g. Input is 14367, Output is 5.
public void isPositive(int num) {
if (num == 0){
System.out.println(1);
} else if (num > 0){
int count = 0;
while (num > 0) {
count++;
num /= 10;
}
System.out.println(count);
} else {
System.out.println("The number should be positive.");
}
}
Comments
Leave a comment