New Tutorials:   KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL  
See the Tutorial List

Program to find Sum of digits of a Number using Recursion

Below is a program to find sum of digits of a given number using recursion.


//declaring the recursive function
int sumOfDigit(int num);

void main()
    int num, sum;
    printf("Enter a number:\t");
    scanf("%d", &num);
    sum = sumOfDigit(num);
    printf("The sum of digits of %d is: %d", num, sum);

int sumOfDigit(int num)
    int s, a;
    s = s + (num%10);
    a = num/10;
    if(a > 0)
    return s;

Enter a number: 108 The sum of digits of 108 is: 9