Java programs to count the digits in an integer have been shown here. For example, if a number is 13579, the total number of digits is 5. The algorithm, pseudocode and time complexity of the programs have also been covered below.
Page content(s):
1. Algorithm to count the digits in an integer
1. Take a number x as input.
2. Initialize a counter to 0 say c = 0.
3. Perform x = x / 10 and Increment c by 1 i.e. c = c + 1
4. If x = 0 stop the process and display c as output else go to step 3.
2. Pseudocode to count the digits in an integer
Input : A number $n$
Output : Total no of digits in $n$
1. Procedure countDigits($n$):
2.
3.
4.
5.
6.
7.
8.
9.
10. End Procedure
3. Time complexity to count the digits in an integer
Time Complexity: O(log(n))
Where n is the input number.
4. Program & output to count the digits in an integer
4.1. Java Program & output to count the digits in an integer using iteration
/******************************************* alphabetacoder.com Java program to count the number of digits in an integer using iterative approach ********************************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare and initialize variables int num = 0, count = 0; // take input System.out.print("Enter the integer = "); num = sc.nextInt(); //count the digits if (num == 0) count = 1; else { while (num != 0) { num = num / 10; count++; } } // display result System.out.print("Number of digits = " + count); } }
Output
Enter the integer = 51265
Number of digits = 5
4.2. Java Program & output to count the digits in an integer using recursion
/******************************************* alphabetacoder.com Java program to count the number of digits in an integer using recursive approach ********************************************/ import java.util.Scanner; class Main { // recursive function to calculate // no of digits in an integer public int count_digit(int num) { if (num / 10 == 0) return 1; else // call function return 1 + count_digit(num / 10); } public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare object of Main class Main obj = new Main(); // declare variables int num; // take input System.out.print("Enter the integer = "); num = sc.nextInt(); // display result by calling recursive function System.out.print("Number of digits = " + obj.count_digit(num)); } }
Output
Enter the integer = 1122
Number of digits = 4
4.3. Java Program & output to count the digits in an integer using logarithm
/************************************ alphabetacoder.com Java program to count the number of digits in an integer using logarithm **************************************/ import java.util.Scanner; import java.lang.Math; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int num; // take input System.out.print("Enter the integer = "); num = sc.nextInt(); // display result if (num == 0) // if input number = 0 System.out.print("Number of digits = 1"); else { // calculate result using log10 System.out.print("Number of digits = " + (int) Math.floor(Math.log10(num) + 1)); } } }
Output
Enter the integer = 3456
Number of digits = 4