62. JAVA Program to find the number of digits of a given number.
How does this program work?
- This program is used to find the number of digits of a given number using JAVA.
- The integer entered by the user is stored in variable n.
- Here we are using while loop and this loop will be iterated upto n not equal to zero, once n will be zero the loop is terminated.
Here is the code
//Numbers of digits.
import java.util.Scanner;public class Digit
{
public static void main(String[] args)
{
int count = 0, num ;
System.out.println("Enter a digit");
Scanner obj= new Scanner(System.in );
num=obj.nextInt();
while(num != 0)
{
num = num/10;
++count;
}
System.out.println("Number of digits: " + count);
}
}