Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Saturday, November 7, 2020

Java Program To Count Number Of Digits In Number

1. Overview

In this programming series, you'll learn how to count the digits in a number in java. This can be done in many ways using for loop with simple iterative approach, recursive, log based and string conversion methods.

Let us jump into the example programs.





2. Example 1 - Count Digits Using Iterative Approach


In this approach, you can use the while loop or for loop to get the each number and run the loop through all digits.


public class CountDigitsIterativeExample {

	public static void main(String[] args) {

		// Input number
		int number = 12345;

		// storing the original number in temp variable
		int originalNumber = number;

		// default digits count
		int countDigits = 0;

		// counting the digits
		while (number > 0) {

			number = number / 10;
			countDigits++;

		}

		// printing the output
		System.out.println("No of digits in number " + originalNumber + " is " + countDigits);

	}

}

Output:
No of digits in number 12345 is 5

3. Example 2 - Count Digits Using Recursive Approach


Next, use the recursive method to find the number of digits in a number.

Recursive means calling a method from the same method until a certain condition is met.

For example, Let us create a method countDigits(int number) and call the same countDigits() from inside the same method.

Look the following code.
public class CountDigitsRecursiveExample {

	public static void main(String[] args) {

		// Input number
		int number = 123456789;

		// calling the recursive method
		int digitsCount = countDigits(number);

		// printing the output
		System.out.println("No of digits in number " + number + " is " + digitsCount);

	}

	/**
	 * returns the count of digits in the given number
	 * 
	 * @param number
	 * @return
	 */
	public static int countDigits(int number) {

		if (number == 0) {
			return 0;
		}

		return 1 + countDigits(number / 10);
	}
}

Output:
No of digits in number 123456789 is 9

4. Example 3 - Count Digits Using Logarithmic Method - Math.log10()


Another way, Math api has a method log10() which returns the log base 10 value for a number. But, this approach does not work for negative numbers.

package com.javaprogramto.programs.count;

public class CountDigitsRecursiveExample {

	public static void main(String[] args) {

		// Input number
		int number = 1234567;

		int count = (int)Math.floor(Math.log10(number) + 1);

		// printing the output
		System.out.println("No of digits in number " + number + " is " + count);

	}
}

Output:
No of digits in number 1234567 is 7

5. Example 4 - Count Digits Convert Integer to String


Final approach is to convert the integer to String using Integer.toString(int n).
package com.javaprogramto.programs.count;

public class CountDigitsRecursiveExample {

	public static void main(String[] args) {

		// Input number
		int number = 123;

		String numberInString = Integer.toString(number);
		
		int length = numberInString.length();

		// printing the output
		System.out.println("No of digits in number " + number + " is " + length);

	}
}

Output:
No of digits in number 123 is 3

6. Conclusion

In this article, you've seen how to count the all digits in a number.


No comments:

Post a Comment

Please do not add any spam links in the comments section.