Write C++ program to find sum of series 1 + 2 + 3 +……+ n
#include
#include
using namespace std;
int main()
{
int i,n,sum=0;
cout<<"1+2+3+......+n";
cout<<"\nEnter the value of n:"; cin>>n;
for(i=1;i<=n;++i)
sum+=i;
cout<<"\nSum="<<sum;
return 0;
}
Today, I will be sharing you a C++ program to find the sum of series 1 + 2 + 3 +….+n.
Let me explain the C++ program with the help of the image.
This is the C++ program to find the sum of series.
Here, we are calculating the sum of series 1 + 2 + 3 +…+n.
First of all, we are declaring two variables, one for storing the number of elements in the series and another for storing the sum of the series.
#include
using namespace std;
int main()
{
int n,s;
cout<<"Enter the number of elements in the series : ";
cin>>n;
for(int i=1;i<=n;i++)
{
s=i;
cout<<s<<" ";<="" p=""></s<<">
}
cout<<endl;< p=""></endl;<>
return 0;
}
After executing the program, it will ask for the number of elements in the series, the user inputs the number and the output will be displayed as shown in the image below.
This program will work for all kinds of series, whether it is series of natural numbers, series of powers, series of square roots, series of cube roots, series of exponential, series of logarithm etc.