C Program to Find Sum of Natural Numbers using Recursion
ADVERTISEMENTS
C program to find sum of natural numbers using recursion. In this article, you will learn how to find sum of natural numbers using recursion in c language.
Source Code
// 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;
}
Output
-----Enter a positive number to get the sum-----
36
The Sum is: 666