Java Program to Count Number of Digits in an Integer

In this post, we will learn to code the Java Program to Count Number of Digits in an Integer. Let’s understand about numbers and “How to count digits of the number in Java Programming Language”

We are going to code the Java Program to Count Number of Digits in an Integer using two approaches
1. Using For Loop
2. Using While Loop

Let’s see the Java Program to Count the Number of Digits in an Integer Using While Loop.

Java Program to Count Number of Digits in an Integer

import java.util.*;

public class Main{
    
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number: ");
        int number = sc.nextInt();
        
        int n = number; // taking the copy of the number
        int count = 0;
        
        while(n > 0){
            n = n / 10;
            count++;
        }
        
        System.out.println("Number of digits in "+number+" are "+count);
        
    }
    
}

Output

Enter the number: 1223343

Number of digits in 1223343 are 7

How Does This Program Work?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number: ");
        int number = sc.nextInt();

In this program, we first took the input of the number using the Scanner Class in Java and then stored it in the variable number of int datatype.

        int n = number; // taking the copy of the number
        int count = 0;

Then, we declared two variables of int datatype. We assigned variable n with the value of variable number and count with 0. We have copied the data of variable number in n because further in the code we are going to manipulate the data so the original number should not be lost.

        while(n > 0){
            n = n / 10;
            count++;
        }

Then, we have created a while loop with a condition till n > 0. In this loop, we are dividing the number by 10 and incrementing the count variable till the value of n becomes 0.

In first iteration, n = 1234
Then, n = 1234/10 = 123 and count = 1, from 0.
Then, n = 123/10 = 12 and count = 2
Then, n = 12/10 = 1 and count = 3
Then, n = 1/10 = 0 and count = 4
Now, n = 0 so the while loop terminates, as n is not greater than 0

This is the Java Program to Count Number of Digits in an Integer Using While Loop.

Now, Let’s see the Java Program to Count Number of Digits in an Integer Using For Loop.

Java Program to Count Number of Digits in an Integer Using For Loop.

import java.util.*;

public class Main{
    
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number: ");
        int number = sc.nextInt();
        
        int n = number; // taking the copy of the number
        int count;
        
        for(count=0 ; n > 0 ; count++){
            n = n / 10;
        }
        
        System.out.println("Number of digits in "+number+" are "+count);
        
    }
    
}

Output

Enter the number: 123456789

Number of digits in 123456789 are 9

How Does This Program Work?

        int count;
        
        for(count=0 ; n > 0 ; count++){
            n = n / 10;
        }

This program logic is very much similar to the program explained above. Here, we have just used the for loop instead of the while loop. We have declared the variable count of int datatype outside the for loop because we need to print the variable hence, declaring the variable outside the loop has increased the scope of the variable count.

Then, in the for loop we initialized count = 0 and given n > 0 as the condition and then incremented the count variable.

This is the Java Program to Count Number of Digits in an Integer Using For Loop.

Conclusion

I hope after going through this post, you understand how to code Java Program to Count Number of Digits in an Integer. We discussed two approaches to solve this problem.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published.