Write a program to find the sum of digits of a given integer using recursion.

This answer is restricted. Please login to view the answer of this question.

Login Now
#include <stdio.h>
 
int sum (int a);
 
int main()
{
    int num, result;
 
    printf("Enter the number: ");
    scanf("%d", &num);
    result = sum(num);
    printf("Sum of digits in %d is %d\n", num, result);
    return 0;
}
 
int sum (int num)
{
    if (num != 0){
        return (num % 10 + sum (num / 10));
    }else{
       return 0;
    }
}

The output of the above program is

Enter the number: 123456
Sum of digits in 123456 is 21
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .