Get number of digits in an integer java: Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Program to Count Number of Digits in an Integer
Java count digits in integer: In this article we will see how to count number of digits in an integer.
For example:
The number 32456 has 5 digits in it. The number 9 has 1 digit in it. The number 6073 has 4 digits in it.
There are multiple ways to do this. Like we can do it by
- By using while loop
- By using for loop
- By using recursion
- By using log based solution
- By using conversion from int to string
Method-1 : By using while loop
Number of digits in a number java: By using the while loop we can count the number of digits in an integer.
This is an iterative approach.
Approach:
- Take an integer variable
number
assigned with an integer value. - Take a while loop and iterate the loop until
number != 0
- And go on dividing the number by 10 on each iteration like
number=number/10
- Keep on counting each digit during that with the help of a variable say ‘
count
‘.
For example:
- number is 4512
- After first iteration number will be number=number/10 i.e number=4512/10 => number=451 and increment count value to 1
- After second iteration number will be number=number/10 i.e number=451/10 => number=45 and increment count value to 2
- After third iteration number will be number=number/10 i.e number=45/10 => number=4 and increment count value to 3
- After fourth iteration number will be number=number/10 i.e number=4/10 => number=0 and increment count value to 4
- In next while check number is 0, so condition failed as iteration will only continue till while(number != 0), so it will not go into the loop again.
- Now print the count value that is 4, so number of digits in the integer is 4.
Program:
public class Main { public static void main(String[] args) { int count = 0, number = 4512; // loop will continue till number value become 0 while (number != 0) { // dividing the number with 10 number=number/10; count++; } // printing the count value System.out.println("Number of digits: " + count); } }
Output: Number of digits: 4
Method-2 : By using for loop
Java number of digits in int: By using the for loop we can count the number of digits in an integer.
This is an iterative approach.
Approach:
- Take a for loop without body.
- continue for loop upto
number != 0
becomes false - Keep on dividing the
number
value in each iteration - In each iteration increment
count
value 1
public class Main { public static void main(String[] args) { int count = 0, number = 4512678; // Loop will continue until number becomes 0 // In each loop number will go on divided by 10 // And keep on counting for (; number != 0; number=number/10, ++count) { } System.out.println("Number of digits: " + count); } }
Output: Number of digits: 7
Method-3 : By using recursion
By using recursion we can count the number of digits in an integer.
Approach:
- Logic is very simple just we need to divide number by 10 with in the user defined method
count()
. - It will check if the number/10==0, if 0 then it will stop there, otherwise it will return 1 and recursion will go on.
- Inside the count() method, recursively count() method will be called. And accordingly return value will be added in that.
Program :
import java.util.*; class Main { // user defined method 'count' static int count(int n) { // dividing number with 10 // if after division n value is not 0 // then return 1 if (n/10 == 0) return 1; // it will return the final digit count // calling the count() method recursively return 1 + count(n / 10); } // main method public static void main(String[] args) { // integer value stored in int variable 'number' int number = 123; // count() method called System.out.print("Number of digits : " + count(number)); } }
Output: Number of digits: 3
Method-4 : By using log based solution
By using log based we can count the number of digits in an integer. But only for positive integers.
Approach:
- By using log10(logarithm of base 10) we will count the number of digits.
- Digit count of number = upper bound of log10(number).
Program :
import java.util.*; class Main { static int count(int num) { // using log approach return (int)Math.floor(Math.log10(num) + 1); } /* Driver code */ public static void main(String[] args) { // intger value stored in int variable 'number' int number = 321; // count() method called System.out.print("Number of digits :" + count(number)); } }
Output: Number of digits: 3
Method-5 : By using conversion from int to string
By converting int to string and counting length we can count the number of digits in an integer. But only for positive integers.
Approch:
- Take a number.
- convert it to string by using
toString()
method. - Then count the length of the string using length() like
num.length()
- We have done this using a user defined method
count()
.
Program:
import java.util.*; public class Main { // user defined method // To count the no. of digits static void count(int n) { // using toString() method converting into to string String num = Integer.toString(n); // getting the length of the string System.out.println("Number of digits :"+num.length()); } // main method public static void main(String args[]) { // numberstored in int variable 'number' int number = 345; count(number); } }
Output: Number of digits: 3
Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.
Related Java Decision Making and Loop Programs:
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Find Factorial of a Number
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Check Palindrome
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Check Armstrong Number
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Make a Simple Calculator Using switch…case
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)