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

Java Programming Java8Object Oriented Programming

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
raja
Published on 26-Apr-2018 08:17:06
Advertisements