TheFestFever.com

Our aim is to connect students from campuses all over Delhi by making them aware of all Technical, Cultural, Management fests, workshops, Seminars organized by every college in Delhi.

Join Us Now

Program 48: Write a C++ program to calculate the sum of numbers from 1 to n using recursion.

#include<iostream>
using namespace std;

int sum(int n)
{
	if(n==1)
		return n;
	else
		return n+sum(n-1);
}

int main()
{
	int num;
	cout<<"\nenter a number : ";
	cin>>num;
	cout<<"\nsum of "<<num<<" natural numbers : "<<sum(num);
	return 0;
}


If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.