Program To Find Sum of Digits of a Number - C Program

Program To Find Sum of Digits of a Number

program to find sum of digits of a number. Example: if number = 3234 then sum of its digits = 3 + 2 + 3 + 4 = 12

#include <stdio.h>
int main()
{
    int num, dsum = 0,r,n;
    printf("Enter the Number :");
    scanf("%d",&num);
    n=num;
    while(n!=0)
    {
        r = n % 10;
        dsum = dsum + r;
        n = n / 10;
    }
    printf("DigitSum of %d is %d", num, dsum);
}

Output - Program To Find Sum of Digits of a Number

Enter the Number :3234                                                                                                
DigitSum of 3234 is 12 
Compile and Run Online