C Program to find Sum of Digits of a given number Type 2
Program
#include<stdio.h>
void main()
{
int rem, num, sum = 0;
printf("Enter a number to find sum of digits\n");
scanf("%d", &num);
rem = num;
while(rem > 0)
{
sum = sum + (rem % 10);
rem = rem / 10;
}
printf("Sum of digits of number %d is %d\n", num, sum);
}
Output
Enter a number to find sum of digits
9421
Sum of digits of number 9421 is 16