C program to find Sum of Digits of a number
#include<stdio.h>
int main()
{
int num, sum=0;
printf("Enter Digits: ");
scanf("%d",&num);
//Repeat till num = 0
while(num!=0)
{
//Find last digit of num and add to sum
sum += num % 10; //sum = sum + (num%10)
//Remove last digit of number
num = num / 10;
}
printf("Sum of Digit: %d",sum);
return 0;
}
Output
Enter Digits: 12345
Sum of Digit: 15