Java Program – Count Number of Digits in a Number
You can count the number of digits in a given number in many ways using Java.
One of the approach is that, we shall take the number, remove the last digit, and increment our counter for number of digits in the number. We can continue this, until there is no digit in the number.
Another approach is that, we can convert the given number into a string, and use the method String.length() to count the number of character in this string. This results in the number of digits in given number.
Example 1 – Count Number of Digits in a Number
In this example, we shall take a number initialized to an integer variable num
, and initialize the count
to zero.
Write a while loop, till while loop is zero, and in the body of while loop, we shall remove the last digit of the number and increment the count.
As the while loop runs for the number of digits in given number num
, the resulting count
value represents the number of digits in the initial num
value.
Example.java
/**
* Java Program - Count Number of Digits in Integer
*/
public class Example {
public static void main(String[] args) {
//number
int num = 5698234;
//variable to store count of digits in number
int count = 0;
//count number of digits in num
while(num!=0) {
num = num/10; //removes last digit of num
count++;
}
//print the count
System.out.println(count);
}
}
Run the above Java program. You shall get the following output in console output.
Output
7
While Loop Execution Explanation
Following snippet shows the values of num
and count
variables during each iteration of the while loop.
num : 5698234
count : 0
Iteration : 1
While condition (num!=0) 5698234!=0 is true
num : 569823
count : 1
Iteration : 2
While condition (num!=0) 569823!=0 is true
num : 56982
count : 2
Iteration : 3
While condition (num!=0) 56982!=0 is true
num : 5698
count : 3
Iteration : 4
While condition (num!=0) 5698!=0 is true
num : 569
count : 4
Iteration : 5
While condition (num!=0) 569!=0 is true
num : 56
count : 5
Iteration : 6
While condition (num!=0) 56!=0 is true
num : 5
count : 6
Iteration : 7
While condition (num!=0) 5!=0 is true
num : 0
count : 7
Iteration : 8
While condition (num!=0) 0!=0 is false
count : 7
Thus we have counted all the digits in a number.
Example 2 – Count Digits in a Number using String
In this example, we shall take a number in an integer variable. Then we shall typecast the number to string by appending an empty string to it num+""
. We shall the call length method on this string (num+"").length()
. lenght() property returns the length of the string. We store this value in a variable count
. Now count contains the value equal to number of digits in the number.
Example.java
/**
* Java Program - Count Number of Digits in Integer
*/
public class Example {
public static void main(String[] args) {
//number
int num = 5698234;
//convert number to string and find the length
int count = (num+"").length();
//print the count
System.out.println(count);
}
}
Run the above Java program, and you shall get the following output.
Output
7
Conclusion
In this Java Tutorial, we learned how to count the number of digits in a given number.