## Java Program to count the digits of a given number using while loop

In this program, we are counting the number of digits present in a given number.

For Example

```Suppose if the given number is say 51

Output should be 2

if the input is 123

Output should be 3```

## Steps to write digits count program

• Here we are using “Scanner” to take input for numbers.
• We are taking a counter variable which will increment in accordance with the number of digits in the number.
• Now we will run the while loop till the given number is 0.
• Inside the loop, we divide the number with 10 as long as it meets the condition and increment the counter variable.
• Once the loop condition breaks, the incremented value of the counter variable is the number of digits in the number.

## Program to count number digits in Java

``````import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int count = 0;
System.out.println("Enter the Number : ");
int number = sc.nextInt();
while(number>0) {
number = number/10;
count++;
}
System.out.println("There are "+count+" digits in the number");
sc.close();
}
}``````

Output 1:

``````Enter the Number :
43
There are 2 digits in the number``````

Output 2:

``````Enter the Number :
4223
There are 4 digits in the number``````