C++ Program to Add First n Numbers using recursion

Write C++ Program to Add First n Numbers using recursion

// CPP Program to Add First n Numbers using recursion

#include <iostream>

using namespace std;

int add(int n)
{
	if(n != 0)
		return n + add(n - 1);
	else
		return n;

}

int main()
{
	int n;

	cout << "Enter n :--> ";
	cin >> n;

	cout << "Sum of First " << n << " Numbers : " << add(n);
	return 0;
}

Output:

Enter n :--> 15
Sum of First 15 Numbers : 120

≪ Previous | Next ≫