Java Program to Count Number of Digits in a Number

Here is a Java program to find the count of digits in a number using loop and logarithm. In this java program, we have to find the number of digits in given integer.

For Example:
Count of digits in 1435238 is 7

Java program to find the count of digits in an integer

In this program, we count the number of digits in N by removing one digit from N at a time in every iteration and increment count until N becomes zero.

Algorithm to count digits of a number
Let N be the given number and "count" be an integer variable for storing number of digits in N.
  • Initialize count to 0.
  • Remove the least significant digit from N. (N = N /10;)
  • Increment count.
  • Repeat above two steps, till N is not equal to zero.
package com.tcc.java.programs;

import java.util.Scanner;

public class CountDigits {
    public static void main(String[] args) {
        int number, count = 0, temp;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("Enter an Integer");
        number = scanner.nextInt();

        temp = number;
        while (temp != 0) {
            temp = temp / 10;
            ++count;
        }

        System.out.format("Number of Digits in %d = %d", 
           number, count);
    }
}
Output
Enter an Integer
63542
Number of Digits in 63542 = 5
Enter an Integer
1
Number of Digits in 1 = 1

Java program for counting digits of a number using logarithm

We first take a number N as input from user and check if it is a negative number. If true, then we multiply it by -1 to make it positive. Here we are using log10(logarithm of base 10) to count the number of digits of N (logarithm is not defined for negative numbers).
Digit count of N = log10(N) + 1

package com.tcc.java.programs;

import java.util.Scanner;

public class CountDigitsLog {
    public static void main(String[] args) {
        int number, count = 0, temp;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("Enter an Integer");
        number = scanner.nextInt();

        temp = number;

        if (temp < 0)
            temp = temp * -1;

        if (temp != 0) {
            /* If input number is non-zero */
            count = (int) Math.log10(temp) + 1;
        } else {
            count = 1;
        }

        System.out.format("Number of Digits in %d = %d", 
           number, count);
    }
}
Output
Enter an Integer
1234
Number of Digits in 1234 = 4
Enter an Integer
1
Number of Digits in 1 = 1

Recommended Posts
Java Program to Find Sum of Digits of a Number
Java Program To Reverse Digits of a Number using While Loop
Java Program to Find Factorial of a Number Using Recursion
Java Program to Find All Factors of a Number
Java Program to Check Perfect Number or Not
Java Program to Reverse a Number using Recursion
Java Program to Find LCM and GCD of Two Numbers
Java Program to Print Multiplication Table of Number
Java Program to Calculate Average and Percentage Marks
Java Program to Check Whether a Number is Palindrome or Not
Java Program to Convert Fahrenheit to Celsius
All Java Programs