Sum of Digits of Number C Program

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.

Recommended Programs
C Program To Find Sum of Digits of a Number using Recursion
C Program To Reverse a String
C Program To Display Current Date and Time
C Code For Decimal Number To Binary Conversion
C Program To Find Maximum Element in Array
100+ C Programs For Programming Interviews

Tushar Soni

I am Tushar Soni, Co - Founder of CodingAlpha. I am a computer science student from India and passionate about Web Development and Programming. Connect with me on Facebook | LinkedIn | Google Plus

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

• July 31, 2016 at 4:22 pm

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

• September 13, 2016 at 2:15 am

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.