C Program to Find Sum Natural Numbers using Recursion

Write a Program to Find Sum Natural Numbers using Recursion

// C Program to Find Sum Natural Numbers using Recursion

#include <stdio.h>

int recSum(int n)
{
	if(n <= 1)
		return n;

	return n + recSum(n - 1);
}

int main()
{
	int n;
	printf("Enter n :--> ");
	scanf("%d", &n);

	printf("1 + 2 + ... + %d = %d", n, recSum(n));

	return 0;
}

Output:

Enter n :--> 6
1 + 2 + ... + 6 = 21

≪ Previous | Next ≫