07 C Program to Compute the Sum of Digits in a given Integer
Posted by Superadmin on December 21 2015 17:18:55
This C Program computes the sum of digits in a given integer. This program m accepts integer. Then adds all the digits of a given integer, that becomes the sum of digits of integer.
Here is source code of the C program to compute the sum of digits in a given integer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to accept an integer & find the sum of its digits
*/
#include
void main()
{
long num, temp, digit, sum = 0;
printf("Enter the number \n");
scanf("%ld", &num);
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp, sum);
}
$ cc pgm81.c
$ a.out
Enter the number
300
Given number = 300
Sum of the digits 300 = 3
$ a.out
Enter the number
16789
Given number = 16789
Sum of the digits 16789 = 31