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

Method 1: C Program To Calculate Sum of Numbers of an Integer with While Loop

Method 2: Find Sum of Digits of Number in C Programming using For Loop

 

Output

C Program To Find Sum of Digits of Number

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

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?

    Reply
  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.

    Reply
    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.

      Reply

Let's Discuss