programming-examples/c/_Basic/C Program to Compute the Sum of Digits in a given Integer.c

20 lines
458 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/*
* C program to accept an integer & find the sum of its digits
*/
#include <stdio.h>
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);
}