C Online Compiler
Example: C Program to Find Sum of Natural Numbers using Recursion
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C Program to Find Sum of Natural Numbers using Recursion #include <stdio.h> int recSum(int x); // It's the driver function int main() { int x; // To store a positive number printf("-----Enter a positive number to get the sum-----\n"); scanf("%d", &x); printf("\nThe Sum is: %d\n", recSum(x)); return 0; } // This function will calculate the sum of natural numbers int recSum(int n) { if (n) return n + recSum(n - 1); else return n; }
36
Output
Clear
ADVERTISEMENTS