Answer to Question #309527 in Java | JSP | JSF for Kyle

Question #309527

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.



1
Expert's answer
2022-03-11T02:26:14-0500
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.");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS