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:

1. Ask the user to input the number.
2. Using the modulus operator, get the remainder of the number.
3. Add the remainder of the number.
4. Perform the division of the number by 10
5. 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.

## 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

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

Introduction to C Programming

Check Leap Year in C Programming

How to Calculate Simple Interest in C Programming

Calculate Profit or Loss in C

Print ASCII Values Using While Loop in C Program

A highly motivated individual with a hunger to learn and share as much as I can about everything. Masters in Interaction Design with a Bachelors in Computer Science. I have worked with clients across the globe on various design and tech projects. I believe in giving back and that's how ReadMeNow was founded.