# Sum of Digits of Number

www.Sillycodes.com

## Program Description:

Write a C program to find the sum of digits of a number.

Here are a couple of examples.

## Sum of digits of number program Logic:

• The basic idea is to get the individual digits of the given number and then sum them up to get the sum of the digits.
• We will maintain one variable sum  to hold the sum of the digits. This sum variable will be initialized to zero.
• We need a way to go through all digits one by one and add them to our “sum” variable.
• We will use the modulus operator and division operator. The modulus operator is very useful to get the last digit of any number. If we do the modulus of any number with 10, We will get the last digit.
• For example. 123%10 = 3
• Similarly, We can remove the last digit from the number by dividing the given number by 10.
• For example:  123/10  = 12
• So we are going to use the above two operators to get the last digit and remove the last digit.
• We will start from the end and continue till we reach the first digit. At each iteration, we are going to get the last digit using the modulus operator and then we will add the last digit to our sum variable. Then we will remove the last digit from the number by using the division operator. And we are going to repeat the above steps until our number becomes less than 0. Which is the stopping condition for our loop.

## Example walk-through – The sum of digits of a number:

Let’s take an example number, num = 1234.

### At the first iteration:

The  sum variable be zero

First of all get last Digit of the number using the modulus oprator.

num%10  i.e  1234%10 = 4

Add the last digit to  sum riable

sum = 0 + 4 --> sum = 4

Remove the last digit from the number.

num = num/10  --> num = 1234 / 10 --> num = 123

After the first iteration, The sum is 4, and the number ( num ) is 123.

### At the second Iteration:

Get last Digit of the num using the modulus oprator.

num%10  i.e  123%10 = 3

Add the last digit to  sum riable

sum = 4 + 3 --> sum = 7

Remove the last digit from the num.

num = num/10  --> num = 123 / 10 --> num = 12

After the second iteration, The sum is 7, and the number ( num ) is 12.

### At Third Iteration:

Get last Digit of the num

num%10  i.e  12%10 = 2

Add the last digit to sum variable

sum = 7 + 2 --> sum = 9

Remove the last digit from the num.

num = num/10  --> num = 12 / 10 --> num = 1

After the Third iteration, The sum is 9, and the number ( num ) is 1.

### At Fourth Iteration:

Get last Digit

num%10  i.e  1%10 = 1

Add the last digit to sum variable

sum = 9 + 1 --> sum = 10

Remove the last digit from The number.

num = num/10  --> num = 1 / 10 --> num = 0

After the Third iteration, The sum is 10, and the number ( num ) is 0.

### At Fifth Iteration:

while loop condition num > 0 be false and loop will terminate.

We will have the  sum variable contains the  sum of digits of the given number (num)

## Related Programs: Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 6 Responses

1. May 24, 2021

[…] C Program to find sum of digits of a Number | Sum of Digits program in C – SillyCodes […]

2. May 27, 2021

[…] C Program to find sum of digits of a Number | Sum of Digits program in C – SillyCodes […]

3. May 29, 2021

[…] C Program to find sum of digits of a Number | Sum of Digits program in C – SillyCodes […]

4. May 31, 2021

[…] C Program to find sum of digits of a Number | Sum of Digits program in C – SillyCodes […]

5. June 1, 2021

[…] C Program to find sum of digits of a Number | Sum of Digits program in C – SillyCodes […]

6. June 3, 2021

[…] C Program to find sum of digits of a Number | Sum of Digits program in C – SillyCodes […]