**Sum of digits in a number** is implementation where we add each single digit in given number.

**Sum of digits of a number in java**

what is sum of digits ? :- First, we have a number given any number of digits in it. We need to add every digit present in number.

**How to sum all digits in a number?**

**Steps:-**

To do this first we have to separate digits present in number.

For separating digits we can use modulus operation (% 10)which gives us a single digit.

After getting single digit we perform sum operation.

We Repeat step 1 and 2 till digits in number end.

**Example**

Suppose the number = 365

**1. Now to perform sum of all digits we have to first separate out each digital. We do this by modulus which gives us reminder ( a digit)**

=> rem = number % 10

=> rem = 365 % 10

=> rem = 5

**2. Add rem (a digit)**

=> sum = sum + rem

=> sum = 5

**3. Repeat step 1 and 2**

=> rem = number % 10

=> rem = 36 % 10

=> rem = 6

**4. Add rem (a digit)**

=> sum = sum + rem

=> sum = 5 + 6

=> sum = 11

**5. Repeated 1 and 2**

=> rem = number % 10

=> rem = 3 % 10

=> rem = 3

**6. Add rem (a digit)**

=> sum = sum + rem

=> sum = 11 + 3

=> sum = 14

## Sum of digits program in java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.*; public class Main { public static void main(String[] args) { int dummy,n,sum=0,x; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextInt(); dummy=n; while(n>0){ x=n%10; sum=sum+x; n=n/10; } System.out.println("The sum of all digits in "+dummy+" is "+sum); } } |

**Output :**

**Explanation**

**1. Here we did initialization for**

dummy ->To store the entered value(i.e ‘n’) as you will come to know at the end of the program

n –>To store number given by a user.

sum->To store the sum of all digits in the number.It is initialized to zero

x->To store n%10.

**2. First of all, we got a number ‘n’ from a user and then stored it in a dummy variable called as ‘dummy’ for restoring the value.(remember this point)**

**3. Now the main logic comes here**

let the number ‘n’ be 321 and as 321>0, while loop gets executed

then x=321%10—>which is 1.

sum=0+1——–>1

n=321/10———>32

The sum for the first loop execution is sum=1.

**4. Now the number ‘n’ has become ’32’ and n>0, while loop executes for the 2nd time**

then x=32%10—>which is 2.

sum=1+2——–>3

n=32/10———>3

The sum when loop executed the second time is sum=3.

Now the number ‘n’ has become ‘3’ and n>0, while loop executes for the 3rd time

then x=3%10—>which is 3.

sum=3+3——–>6

n=3/10———>0

The sum when loop executed the third time is sum=6.

Now as the number of variable ‘n’ is 0 which is not n>0 then the loop terminates.Then the final sum is ‘6’.

So now I hope you understood why the dummy variable is used.It is because the value in ‘n’ becomes 0 at the end of the program so for restoring this value to print at the end we used ‘dummy'(as from the 2nd point).

**5. Finally, it prints the value in ‘sum’.**

## Leave a Reply