C Program to find Sum of Digits of a Number using Recursion

0

Code

#include <stdio.h>
 
int sumOfDigits(int num);
 
int main()
{
    int num, sum;
     
    printf("Enter any number to find sum of digits: ");
     
    scanf("%d", &num);
     
    sum = sumOfDigits(num);
     
    printf("Sum of digits of %d = %d", num, sum);
     
    return 0;
}
 
int sumOfDigits(int num)
{
      if(num == 0)
   
        return 0;
         
    return ((num % 10) + sumOfDigits(num / 10));
}

Output

Enter any number to find sum of digits: 23232
Sum of digits of 23232 = 12

Leave a Reply

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