This article will explain different methods of counting the total number of digits in a given integer in java. Integer value will be read as input from the user.

Method 1: Using repeated division
This method performs repeated division of a number by 10 and is based on the following algorithm.

1. Divide the number by 10.
2. Assign the result back to the number so that it is reduced by 1 digit.
3. Increment a counter.
4. Repeat steps 1 to 3 till the number becomes 0.

This algorithm relies on the logic that the digits in a number are equal to the number of times it is divided by 10.
Java program based on the above algorithm is given below.

import java.util.Scanner;

public class DigitCounter {

// main method
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.println("Enter a number.");
int number = scanner.nextInt();
// store result
int numberOfDigits = 0;
// iterate till number becomes 0
while (number > 0) {
// divide number by 10
number = number / 10;
// increment digit count
++numberOfDigits;
}
System.out.println("Number of digits = " + numberOfDigits);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}
}

Above program produces below output

Enter a number
34798
Number of digits = 5

Method 2: Using recursion
This is a recursive equivalent of the above method. This method creates a recursive method which accepts the number as argument.
This method should return an integer which is the count of digits in the number.
In each recursive call, this method adds 1 and calls itself with number divided by 10 as argument.
Remember that you need to check if the argument is 0, then this method should return 0.
Thus, this method will get called till the number divided by 10 is greater than 0 and will keep on adding 1.
Finally when the recursion ends, the result will be the number of times this method was called or the number of digits in the supplied number.
Program written on above logic is as follows.

import java.util.Scanner;

public class DigitCounter {

// main method
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.println("Enter a number.");
int number = scanner.nextInt();
// call recursive method
int numberOfDigits = count(number);
System.out.println("Number of digits = " + numberOfDigits);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}

static int count(int number) {
// check if number is 0
if(number == 0) {
return 0;
}
// add 1 and call this method again
return 1 + count(number/10);
}
}

Output of this program is

Enter a number
273956
Number of digits = 6

Do not forget to check for the argument(or number) to be 0 else the method will keep on executing forever resulting in java.lang.StackOverflowError.

Method 3: Converting to String
Convert the entered number to a String using String.valueOf method which accepts an integer as argument and then find the length of the string using length() method.
This length will be the number of digits in the number.
Java program written according to this logic is given below.

import java.util.Scanner;

public class DigitCounter {

public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.println("Enter a number.");
int number = scanner.nextInt();
// convert number to string
String str = String.valueOf(number);
// get length of string
int numberOfDigits = str.length();
System.out.println("Number of digits = " + numberOfDigits);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}

}

Output of this program is

Enter a number
3435234
Number of digits = 7

Method 4: Using logarithm
Taking logarithm base 10 of a number can return the number of digits in the number.
Log base 10 of a number a is the power to which 10 must be raised to get that number. That is,

log10a = b means a = 10b

So, log101000 = 103 = 3
log10120 = 2.079
From the above examples, it is clear that if we calculate log10 of a number, round it to an integer and add 1 to it, result is the same as the number of digits in the number.
This approach can be used to calculate the digits in a number.
java.lang.Math class has a log10 method which returns log10 of the value supplied to it as argument. Casting it to an int and adding 1 to the result will return the number of digits in the number.
Java program is given below.

public class DigitCounter {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.println("Enter a number.");
int number = scanner.nextInt();
int numberOfDigits = (int)(Math.log10(number) + 1);
System.out.println("Number of digits = " + numberOfDigits);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}
}

Output of above program is

Enter a number
3545423
Number of digits = 7

Method 5: Repeated product
As opposed to Method 1 where the number was divided by 10 repeatedly, this method works by multiplying by 10 again and again till the result becomes greater than the number.
Total number of times for which the multiplication takes place till the result becomes greater than the number will be the count of digits in the number.
Program to demonstrate this method is given below.

import java.util.Scanner;

public class DigitCounter {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.println("Enter a number.");
int number = scanner.nextInt();
// store result
int numberOfDigits = 0;
int product= 1;
// iterate till number becomes less than the product
while (start < number) {
// multiply number by 10
product = product * 10;
// increment digit count
++numberOfDigits;
}
System.out.println("Number of digits = " + numberOfDigits);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}
}

Output of this program is given below

Enter a number
656434
Number of digits = 6

Click the clap if the article was useful.

0
Liked the article ? Spread the word...