## Table of Contents

**Java program to find the sum of digits of a number.** Finding the sum of the digits of a number is a common exercise in programming. This task involves repeatedly extracting each digit from the number and summing them up. Here’s a Java program that prompts the user to enter a number and then calculates the sum of its digits.

### Java Program to Find the Sum of Digits of a Number

```
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Calculate the sum of the digits
int sum = sumOfDigits(number);
// Print the result
System.out.println("The sum of the digits of " + number + " is " + sum + ".");
}
// Method to find the sum of digits
public static int sumOfDigits(int n) {
int sum = 0;
while (n != 0) {
sum += n % 10; // Add the last digit to the sum
n /= 10; // Remove the last digit
}
return sum;
}
}
```

Top 100 Java Programs: Click Here

Are you looking for a job: Click Here

### Explanation of the Code

Let’s break down the program step-by-step to understand how it works.

**Importing the Scanner Class**: We import the`Scanner`

class from the`java.util`

package to read input from the user.

```
import java.util.Scanner;
```

2.

**Creating the Main Class**: We create a public class named`SumOfDigits`

. In Java, the class name should match the filename.

```
public class SumOfDigits {
```

3. **Main Method**: The `main`

method is the entry point of the program. Inside this method, we will write the code to find the sum of the digits of a number.

```
public static void main(String[] args) {
```

4. **Creating a Scanner Object**: We create a `Scanner`

object to read input from the user.

```
Scanner scanner = new Scanner(System.in);
```

5. **Reading Input from the User**: We prompt the user to enter a number and store this value in a variable.

```
System.out.print("Enter a number: ");
int number = scanner.nextInt();
```

6. **Calculating the Sum of the Digits**: We call the `sumOfDigits`

method to calculate the sum of the digits of the number. The result is stored in a variable `sum`

.

```
int sum = sumOfDigits(number);
```

7. **Printing the Result**: We print the sum of the digits.

```
System.out.println("The sum of the digits of " + number + " is " + sum + ".");
```

8. **Method to Find the Sum of Digits**: The `sumOfDigits`

method calculates the sum of the digits of the number. This method works by repeatedly extracting the last digit of the number and adding it to the sum, and then removing the last digit from the number.

```
public static int sumOfDigits(int n) {
int sum = 0;
while (n != 0) {
sum += n % 10; // Add the last digit to the sum
n /= 10; // Remove the last digit
}
return sum;
}
```

## Java program to find the sum of digits of a number

### Explanation of the `sumOfDigits`

Method

**Initializing the Sum**: We initialize a variable`sum`

to 0. This variable will store the sum of the digits.

```
int sum = 0;
```

2. **Loop to Sum the Digits**: The `while`

loop continues as long as `n`

is not zero. In each iteration, we extract the last digit of `n`

using `n % 10`

, add it to `sum`

, and then remove the last digit from `n`

by dividing it by 10.

```
while (n != 0) {
sum += n % 10; // Add the last digit to the sum
n /= 10; // Remove the last digit
}
```

3. **Returning the Sum**: After the loop finishes, `sum`

contains the sum of the digits. We return `sum`

.

```
return sum;
```

### Running the Program

To run the program, follow these steps:

- Save the code in a file named
`SumOfDigits.java`

. - Open a command prompt or terminal and navigate to the directory where you saved the file.
- Compile the program using the following command:

```
javac SumOfDigits.java
```

4. Run the compiled program using the following command:

```
java SumOfDigits
```

5. Follow the prompt to enter a number. The program will display the sum of its digits.

### Example Output

If you run the program and enter 1234 as the number, the output will be:

```
Enter a number: 1234
The sum of the digits of 1234 is 10.
```

If you run the program and enter 5678 as the number, the output will be:

```
Enter a number: 5678
The sum of the digits of 5678 is 26.
```

### What did we learn from this article?

Finding the sum of the digits of a number is a fundamental exercise that helps in understanding loops and arithmetic operations in programming. This Java program demonstrates how to calculate the sum of the digits by extracting each digit and summing them up. By practicing such basic programs, you can strengthen your problem-solving skills and gain confidence in your ability to write efficient Java code. Keep practicing and exploring more problems to enhance your programming skills.