ComputerShikshak.com

C - Sum of Digits of a Number

Problem Statement

Write a program in C to find the sum of the digits present in an integer number entered by the user.

Brief Description

According to the problem statement we have to write a program in C that will take an integer value as input from the user through the keyboard and will find the sum of the digits present in that number.

For example, if the user enters 132 as input, the program will generate 6 as output since the number 132 contains the digits 1, 3 and 2 and 1+3+2=6.

Program

				
					#include<stdio.h>
#include<math.h>
void main()
{
    int num,temp,rem,sum=0;
    printf("Enter a number: ");
    scanf("%d",&num);
    temp=abs(num);
    while(temp!=0)
    {
        rem=temp%10;
        sum=sum+rem;     //OR sum+=rem;
        temp=temp/10;     //OR temp/=10;
    }
    printf("Sum of the digits present in %d is %d",num,sum);
}
				
			

Output

When the program given above is executed using Dev-C++ IDE we get the following output:

Explanation

In this program:
  • 4 integer variable namely num, temp, rem and sum are declared.
  • Initially 0 is stored in sum.
  • The integer value entered by the user is stored in num.
  • The absolute value corresponding to the entered value is stored in temp.
The step-by-step working of the while loop (when num and temp both contains 132) is shown in the table given below:
numtemp!=0rem=temp%10;remsum=sum+rem;sumtemp=temp/10;tempComment
132----0-132Initial Values
132132!=0
↓
1(True)
rem=132%10;2sum=0+2;2temp=132/10;13Iteration 1
13213!=0
↓
1(True)
rem=13%10;3sum=2+3;5temp=13/10;1Iteration 2
1321!=0
↓
1(True)
rem=1%10;1sum=5+1;6temp=1/10;0Iteration 3
1320!=0
↓
0(False)
------While
loop
stops
working
as
temp!=0
is
evaluated
to
0(False)
While executing the while loop:
  • At first value of temp is checked to see whether it is 0 or not.
  • The condition temp!=0 will produce either 1(True) or 0(False) after evaluation.
    • If it is 1(True), the statements written within while loop are executed and the loop continues to iterate till the condition is 1(True).
    • If it is 0(False) then the statements written within while loop are not executed and the control goes to the statement written immediately after the loop.
  • Within while loop, at first the last digit is extracted from the value stored in temp by performing modulo division operation using the expression temp%10. Thus the expression temp%10 will give the remainder after integer division which is the last digit of the value stored in temp and the result is stored in rem. 

  • After that the value stored in rem is added with the value stored in sum and the result is stored in the same variable i.e. sum. 

  • The value stored in temp is divided by 10 to remove the last digit of the value stored in it and the result is stored in the same variable i.e. temp.

Thus the above process is used to find the sum of the digits present in the given number.

The final result is stored in sum. 

Finally the sum of the digits present in the entered integer value along with the value is displayed on the output screen.

Share this page on
Scroll to Top