C Program to Find Sum of Digit of Number Using Recursive Function

This C program finds sum of digit of a given number by user using recursive function call.


#include<stdio.h>

int digitSum(int );

int main()
{
    int number, sum;

    printf("Enter number: ");
    scanf("%d", &number);

    sum = digitSum(number);

    printf("Sum of digit of %d is %d.", number, sum);
    return 0;

}

int digitSum(int n)
{
    if(n < 10)
    {
        return n;
    }
    else
    {
        return n%10 + digitSum(n/10);
    }
}

Output

Enter number: 976
Sum of digit of 976 is 22.