Java program to Count the number of digits in a given integer


Read a number from user. Create an integer (count) initialize it with 0. Divide the number with 10. till the number is 0 and for each turn increment the count.

Example

import java.util.Scanner;
public class CountingDigitsInInteger {
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      int count = 0;
      System.out.println("Enter a number ::");
      int num = sc.nextInt();
      while(num!=0){
         num = num/10;
         count++;
      }
      System.out.println("Number of digits in the entered integer are :: "+count);
   }
}

Output

Enter a number ::
1254566
Number of digits in the entered integer are :: 7

Updated on: 13-Mar-2020

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements