Program to Count Number of Digits in an Integer

To find Count Number of Digits in an Integer, we are perform following step.

  • Initial a default variable count.
  • To count number of digits of input value n.
  • Inside While loop first check input value not equal to zero.
  • Now divide the input value n by 10 and storing from n.
  • here how many times while loop execute at that times count will be increment.
  • Now print count value.
				
					public class NumberOfDigits {

	public static void main(String[] args) {
		int count = 0, n = 1256;
		while (n != 0) {
			n = n / 10;
			++count;
		}
		System.out.println("Number of digits :- " + count);
	}
}
				
			

Output :-
Number of digits :- 4