C Program Sum of Digits by Recursion

By | March 9, 2020

C Program Sum of Digits by Recursion

Output:

Please, Enter a number to calculate sum of digits by Recursion: 567
Using Recursion, Sum of digits of 567 = 18

C Program to calculate Sum of Digits of a given number.

/**
 ** C program to find sum of digits using recursive function
 */
 
#include <stdio.h>

/* Function prototype / declaration */
int sumDigits(int num);


/* main function*/
int main()
{
    int num, sum;
    
    printf("Please, Enter a number to calculate sum of digits by Recursion: ");
    scanf("%d", &num);
    
    sum = sumDigits(num);
    
    printf("Using Recursion, Sum of digits of %d = %d", num, sum);
    
    return 0;
}


/**
 ** Recursive function to find sum of digits of a number
 **/
int sumDigits(int n)
{
    /* Base condition / termination condition*/
    if(n == 0)
        return 0;
        
    return ((n % 10) + sumDigits(n / 10));
}

Leave a Reply

Your email address will not be published. Required fields are marked *