Input a number and count the number of digits in it using while loop in Java
while Loop - Question 11
In this question, we will see how to input a number and count the number of digits in it in Java programming using while loop. To know more about while loop click on the while loop lesson.
Q11) Write a program in Java to input a number and count the number of digits in it using while loop.
Program
import java.util.Scanner;
public class Q11
{
public static void main(String args[])
{
int n,c=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any number ");
n=sc.nextInt();
while(n>0)
{
c=c+1;
n=n/10;
}
System.out.println("Total Number of Digits = " + c);
}
}
Output
Enter any number 42638 Total Number of Digits = 5