Java program to count number of digits in an integer

http‮ww//:s‬w.theitroad.com

Here's a Java program to count the number of digits in an integer:

public class Main {
    public static void main(String[] args) {
        int num = 123456;
        int count = 0;

        while (num != 0) {
            num /= 10;
            ++count;
        }

        System.out.println("Number of digits: " + count);
    }
}

In this program, we first initialize an integer variable num with the value 123456, and we also initialize an integer variable count with the value 0.

We then use a while loop to count the number of digits in num. In each iteration of the loop, we divide num by 10 and then increment the count variable. We continue to do this until num becomes 0.

Finally, we print out the value of count, which represents the number of digits in the original integer. In this case, the output will be:

Number of digits: 6

Note that this program assumes that the input integer is positive. If the input integer can be negative or zero, you'll need to modify the program to handle these cases appropriately.