C Online Compiler
Example: Sum of Digits of a Number in C using Recursion
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Sum of Digits of a Number in C using Recursion #include <stdio.h> // Recursive function to make the sum of each digit int rec_sum_digits(int num, int sum, int mod) { mod = num % 10; sum += mod; num = num / 10; if (num > 0) rec_sum_digits(num, sum, mod); else return sum; } // It's the main function of the program int main() { int num, sum = 0, mod = 0; // Step-1 Enter a Number printf("Enter Number: "); scanf("%d", &num); // Step-2 Call the recursive function rec_sum_digits() to get the sum of each digits printf("\nOutput: %d", rec_sum_digits(num, sum, mod)); return 0; }
1298
Output
Clear
ADVERTISEMENTS