A program to find the sum of first n natural number using recursion in C++

#include<iostream>
#include<conio.h>
using namespace std;
int sum(int);
main()
{
int n, res;
cout<<“enter the number of terms:”;
cin>>n;
res=sum(n);
cout<<“Sum=”<<res;
getch();
}
int sum(int n)
{
if(n==1)
{
return 1;
}
else
{
return(sum(n-1)+n);
}
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *