# Sum of Digits of Number C Program

By | September 16, 2015

## C Program To Calculate Sum of Digits of Number

Learn How To Calculate Sum of Digits of Number in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code. This Sum of Digits of a Number C Program makes use of While and For Loops and demonstrates working with User Defined function as well.

#### Algorithm To Find Sum of Digits of a Number

• Fetch the Number from the User
• Get the Last Digit of a Number using Modulus Operator
• Add the Last Digit in the Sum variable
• Now, Remove the Last Digit by using Division Operator

You need to perform the above operations in a loop till the condition (num > 0) is True. This method will fetch you the Sum of Digits of a Number entered by the User.

#### Example

Sum of Digits of 1234 = 10

#### Output

In case you get any compilation errors in this C Program To Calculate Sum of Digits of Number or you have any doubts about it, let us know about it in the Comment Section below.

## 4 thoughts on “Sum of Digits of Number C Program”

1. Sneha Mishra

Why do we have to use Modulus Operator in this C Program to calculate addition of digits in the integer?

2. Jas Arora

Thanks! I finally understood the difference between how a for loop works and a while loop works with this Sum of Digits code in C programming.

3. Mamta Kulkarni

Is it important to intialize the sum variable to zero?

1. Tushar Soni Post author

Yes. It is important to initialize the sum to zero because by default, the automatic variables in C contains garbage values. It will not give an accurate answer if the sum is not zero at the start.