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
long num, temp, digit, sum = 0;
printf("Enter the number \n");
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
Sanfoundry Global Education & Learning Series – 1000 C Programs.