Introduction
Finding the sum of the digits of a number is a common programming task, especially useful in problems related to number theory and mathematics. This task involves breaking down a number into its individual digits and summing them up. This guide will walk you through writing a Java program that calculates the sum of the digits of a given number.
Problem Statement
Create a Java program that:
- Prompts the user to enter a number.
- Calculates the sum of the digits of the number.
- Displays the result.
Example:
- 
Input: 1234
- 
Output: "The sum of the digits of 1234 is 10"
- 
Input: 987
- 
Output: "The sum of the digits of 987 is 24"
Solution Steps
- Read the Number: Use the Scannerclass to take the number as input from the user.
- Calculate the Sum of Digits: Use a loop to extract each digit and sum them up.
- Display the Result: Print the sum of the digits.
Java Program
// Java Program to Find the Sum of Digits of a Number
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
public class SumOfDigits {
    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 a number: ");
            int number = scanner.nextInt();
            
            // Step 2: Calculate the sum of digits
            int sum = calculateSumOfDigits(number);
            
            // Step 3: Display the result
            System.out.println("The sum of the digits of " + number + " is " + sum);
        }
    }
    
    // Method to calculate the sum of digits of a number
    public static int calculateSumOfDigits(int number) {
        int sum = 0;
        
        while (number != 0) {
            // Extract the last digit of the number
            int digit = number % 10;
            // Add the digit to the sum
            sum += digit;
            // Remove the last digit from the number
            number /= 10;
        }
        
        return sum;
    }
}
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: Calculate the Sum of Digits
- The calculateSumOfDigits()method calculates the sum of the digits:- The loop runs as long as the number is not zero.
- In each iteration, the last digit of the number is extracted using number % 10.
- The extracted digit is added to the sum.
- The last digit is then removed from the number using integer division (number /= 10).
 
Step 3: Display the Result
- The program prints the sum of the digits using System.out.println().
Output Example
Example 1:
Enter a number: 1234
The sum of the digits of 1234 is 10
Example 2:
Enter a number: 987
The sum of the digits of 987 is 24
Conclusion
This Java program demonstrates how to find the sum of the digits of a given number. It covers essential concepts such as loops, arithmetic operations, and user input handling, making it a valuable exercise for beginners learning Java programming.