
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the Number of Digits in a Given Integer in Java
Suppose an integer number is given as an input, our task is to write a Java program to count the number of digits in that integer. For this problem, create a counter variable and initialize it with 0. Divide the given integer value with 10 till the integer becomes 0 and increment the counter variable for each turn.
Using while Loop
A while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. For the given problem, we use this loop to check whether the specified integer value is 0 or not. If the value is not zero, we divide the given integer value with 10 and increment the count variable.
When the integer becomes zero, the loop will stop, and the count variable will hold the total number of digits.
Example
The following Java program shows how to count number of digits in a given integer using while loop.
public class CountingDigitsInInteger { public static void main(String args[]) { // to store the number of digits int count = 0; // given integer int num = 1254566; System.out.println("given number:: " + num); // loop to count number of digits while(num != 0){ num = num / 10; count++; } System.out.println("Number of digits in the given integer are:: " + count); } }
Following is the output of the above code ?
given number:: 1254566 Number of digits in the given integer are:: 7
Using String.valueOf() Method
The valueOf() method is available in the String class of java.lang package. It is used to return the string representation of the passed argument. The argument could be any datatype like integer, character, or double.
In this approach, we use the String.valueOf() method to convert the integer into its corresponding string representation and then, we find its length which will give the number of digits.
Example
Let's see the practical use of String.valueOf() method in counting number of digits.
public class CountingDigitsInInteger { public static void main(String args[]) { // given integer int num = 254668899; System.out.println("given number:: " + num); int count = String.valueOf(num).length(); System.out.println("Number of digits in the given integer are:: " + count); } }
Following is the output of the above code ?
given number:: 254668899 Number of digits in the given integer are:: 9
Using Recursion
This is the optimal solution to count the number of digits in a given integer. In recursive approach, we create a method that will call itself till the given integer variable will not become zero. During each call, it will divide the integer and increment the count.
Example
In this Java program, we are using the concept of recursion to count number of digits in a given integer.
public class CountingDigitsInInteger { public static int counting(int num) { if (num == 0) { return 0; } return 1 + counting(num / 10); } public static void main(String[] args) { // given integer int num = 25468877; System.out.println("given number:: " + num); int count = counting(num); System.out.println("Number of digits in the given integer are:: " + count); } }
Following is the output of the above code ?
given number:: 25468877 Number of digits in the given integer are:: 8