In this programming tutorial, we are going to write a C program to calculate the sum of digits of an inputted number. We will be referring to an awesome book called “Let us C” by “Yashwant Kanetkar” and this is the question ‘g’ of the section [I]. We will be posting more and more programs with time. So, please refer the portal for more. If you have any problems with the program or need any assistance, don’t hesitate to write to us. We try to answer as quickly as possible.

## Question:

If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus (%) operator.)

## Solution to calculate the Sum of Digits in C:

This program might seem tricky at first but after some thought, you can easily execute the program. Before we begin, let’s understand the algorithm:

- Ask the user to input the number.
- Using the modulus operator, get the remainder of the number.
- Add the remainder of the number.
- Perform the division of the number by 10
- Loopback to step number 2 till you get the final remainder as 0.

**Note: **Using an integer as your variable will only perform the sum on numbers till 65535. Hence, use a long datatype.

### Download the source code:

Download Program## Program to calculate the sum of digits in C:

//C Program to calculate the sum of digits in a 5 digit number #include<stdio.h> int main() { int number,sum=0,remainder; printf("Enter a 5 digit number whose sum you want to calculate: "); scanf("%d",&number); while(number>0) { remainder=number%10; sum=sum+remainder; number=number/10; } printf("Sum of the numbers is %d",sum); return 0; }

## Output

Enter a 5 digit number whose sum you want to calculate: – 12345

Sum of the numbers is 15

### Download the source code:

Please don’t forget to subscribe to us before you hit the download button! It helps us grow and provide more content.

Download Program## Read Also:

Check Leap Year in C Programming