C++ Program To Find Sum Of The Following Series 1+2+3+4+5+6 . . . . . n
All Answers
need an explanation for this answer? contact us directly to get an explanation for this answer
Using For Loop
#include<iostream>
using namespace std;
int main()
{
int i,n,sum=0;
cout<<"\n1+2+3+4+5+6+……+n\n";
cout<<"\nEnter The Value Of N:\n";
cin>>n;
for(i=1;i<=n;++i)
{
sum+=i;
}
cout<<"\nSum = "<<sum<<endl;
return 0;
}
Method 2:- Using Formula
#include<iostream>
using namespace std;
int main()
{
//By-Ghanendra Yadav
int i,n,sum=0;
cout<<"\n1+2+3+4+5+6+……+n\n";
cout<<"\nEnter The Value Of N:\n";
cin>>n;
sum=(n*(n+1))/2;
cout<<"\nSum = "<<sum<<endl;
return 0;
}
Output:
1+2+3+4+5.....+n
Enter the value of n:
10
sum=55
need an explanation for this answer? contact us directly to get an explanation for this answertotal answers (2)
Using Formula
Output:
1+2+3+4+5.....+n
Enter the value of n:
10
sum=55
need an explanation for this answer? contact us directly to get an explanation for this answer