In this article, we are going to understand and implement a simple Java program, in which, we are going to calculate the sum of all the digits in the given number. In this program, we would have a number, and simply we are going to calculate the sum of all the digits in the given number. Have a look at the below examples, through which, we can get a clearer idea about the input and output.

Examples –

Input: 1234

Output: The sum of all the digits: 10

Input: 1878

Output: The sum of all the digits: 24

Input: 17828:

Output: The sum of all the digits: 26

As you can see from the above examples, we have to take some number as user input, and then we are getting the output as the sum of all the digits in the given number. Now, we are going to have a look at how can we write a java program to achieve the given task. We would first have a look at the entire program, and then we would try to explain the program, so as to understand the concept.

## Sum of Digits of a Number in Java

import java.util.Scanner;public class Main {

public static void main(String[] args) {

int number;

Scanner scanner = new Scanner(System.in);

System.out.print(“Please enter a number:”);

number = scanner.nextInt();

int temp = number;

int mysum = 0;

while (temp!=0)

{

int remainder = temp%10; // with this, we get the last digit from the number

mysum +=remainder; // we add the remainder to the value in mysum

temp/=10; // we get rid of it by dividing the number by 10.

}

System.out.println(“The sum of all the digits in ” + number + ” is ” + mysum);

}

}

As you can see in the above program, we are getting the number from the user, and then we are getting the sum of all the digits in the given number as output. Let’s try to have a look at the output of the above program, and then we would try to understand the above program.

Output examples –

Example 1 –

Please enter a number:161667

The sum of all the digits in 161667 is 27

Example 2 –

Please enter a number:12345

The sum of all the digits in 12345 is 15

Example 3 –

Please enter a number:17267

The sum of all the digits in 17267 is 23

As you can see in the output, we have completed the required task. We have given some number as user input, and then we have the sum of all the digits of the number as output. Now, let’s try to understand the program, through the below steps.

- First of all, we are getting the number as user input, and for that, we have created the Scanner class object, and we are getting the required user input.
- After that, we are creating another variable temp, in which, we are storing the same value as the user input number, so as to have a copy of the data at the end of the program.
- Then we are creating another variable mysum, and this variable is going to hold the sum of all the digits in the given number in the end.
- After this, we are looping till the value in the temp is not zero. Within the loop, we are doing some things repeatedly. Let’s have a look at them now –
- First of all, we are getting the last digit of the number, by getting the remainder while we divide the number by 10.
- Once we have the last digit, we add it to the mysum variable.
- Now, when we have added the last digit, we just need to get rid of that last digit, so that we can move to the next digit. So, we divide the value between the temp by 10 and store the result back to temp.

- When the number becomes zero, we are out of the while loop.
- In the end, we have the sum of all the digits in the given number stored in the mysum variable, which we are giving as an output.

From the above simple steps, we can understand the program in a simple way. We have understood and implemented a simple Java program, to calculate the sum of all the digits in the given number.

#### Conclusion

Through this article, we have understood and implemented a simple Java program, to calculate the sum of all the digits in the given number. You can try to execute the above program, and also you can try other programs similar to this.

#### FAQ About Sum of Digits of a Number in Java

### Q: How to get the last digit of some numbers?

**Ans: **To get the last digit of some given number, we just need to get the remainder while we divide the number by 10.

### Q: How do we get the first digit of some number?

**Ans: **In order to get the first digit of some given number, we just need to divide the number by 10, till the number becomes less than 10. Once the number is less than 10, we are just left with a digit, which is our first digit.

### Q: How to calculate the sum of all the digits of a given number?

**Ans:** In order to calculate the sum of all the digits of a number, we just need to get the last digit first and add the value to wherever you are storing the sum, and since we have now added the last digit, we need to get rid of the last digit so that we can move ahead. Once the number becomes zero, we have the sum of all the digits in the given number