Introduction
Counting the number of digits in a number is a basic yet important task in programming, especially when dealing with numerical data processing. This guide will walk you through writing a Java program that counts the number of digits in a given integer.
Problem Statement
Create a Java program that:
- Prompts the user to enter an integer.
- Counts the number of digits in the integer.
- Displays the result.
Example:
-
Input:
12345 -
Output:
"The number 12345 has 5 digits" -
Input:
987654321 -
Output:
"The number 987654321 has 9 digits"
Solution Steps
- Read the Number: Use the
Scannerclass to take the integer as input from the user. - Count the Number of Digits: Use a loop to repeatedly divide the number by 10 until it becomes 0, counting the number of iterations.
- Display the Result: Print the count of digits.
Java Program
// Java Program to Count the Number of Digits in a Number
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
public class DigitCounter {
public static void main(String[] args) {
// Step 1: Read the number from the user
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
// Step 2: Count the number of digits
int count = countDigits(number);
// Step 3: Display the result
System.out.println("The number " + number + " has " + count + " digits.");
}
}
// Method to count the number of digits in a number
public static int countDigits(int number) {
int count = 0;
// If the number is 0, it has 1 digit
if (number == 0) {
return 1;
}
// Convert the number to positive if it's negative
number = Math.abs(number);
while (number > 0) {
number /= 10; // Remove the last digit
count++; // Increment the digit count
}
return count;
}
}
Explanation
Step 1: Read the Number
- The
Scannerclass is used to read an integer input from the user. ThenextInt()method captures the input number.
Step 2: Count the Number of Digits
- The
countDigits()method calculates the number of digits:- The method initializes a counter
countto 0. - If the number is 0, the method returns 1, since 0 has one digit.
- If the number is negative, it is converted to positive using
Math.abs(). - The loop runs as long as the number is greater than 0, dividing the number by 10 in each iteration (which effectively removes the last digit) and incrementing the digit count by 1.
- The method initializes a counter
Step 3: Display the Result
- The program prints the number of digits using
System.out.println().
Output Example
Example 1:
Enter an integer: 12345
The number 12345 has 5 digits.
Example 2:
Enter an integer: 987654321
The number 987654321 has 9 digits.
Example 3:
Enter an integer: 0
The number 0 has 1 digit.
Conclusion
This Java program demonstrates how to count the number of digits in a given integer. It covers essential concepts such as loops, conditional statements, arithmetic operations, and handling both positive and negative integers, making it a valuable exercise for beginners learning Java programming.