C Program to Calculate Sum of Digits of a Number Using Recursion
Recursion in C means a function calling itself. In the program below the function finds the sum of digits by recursively passing the quotient after dividing the number by 10 until the number becomes 0.
- //C program to Calculate sum of digits using recursion
-
- Description-
-
- #include <stdio.h>
- int sum (int n);
- int main()
- {
- int number, res;
-
- printf("Enter the number: ");
- scanf("%d", &number); //taking input
- res = sum(number); //calling the recursive function
- printf("Sum of digits in %d is %d\n", number, res);
- return 0;
- }
-
- int sum (int number) //recursive function for summation
- {
- if (number != 0)
- {
- return (number % 10 + sum (num/ 10));
- }
- else
- {
- return 0;
- }
- }
Output

Solution not working or have any suggestions? Please send an email to [email protected]
Download Android App