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

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

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
    Permalink

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

    Reply
  • September 13, 2016 at 2:15 am
    Permalink

    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
  • October 1, 2016 at 2:00 pm
    Permalink

    Is it important to intialize the sum variable to zero?

    Reply
    • October 1, 2016 at 2:02 pm
      Permalink

      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

Join The Discussion