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> // This function will make sum of digits of number itself // Using the recursion int DigitSum(int x) { if (x == 0) return 0; return (x % 10 + DigitSum(x / 10)); } // It's the driver function int main() { int x; printf ("Enter the integer number::\n"); scanf ("%d", &x); printf ("The sum of %d digits is = ", x); printf("%d\n", DigitSum(x)); return 0; }
12345
Output
Clear
ADVERTISEMENTS