Monday, 16 November 2015

C++ Program To Find Sum Of The Following Series 1+2+3+4+5+6 . . . . . n

Problem :- Write A C++ Program To Find Sum Of The Following Series 1+2+3+4+5+6 . . . . . n .

Logic :- This is very simple series you just need to print sum of 1 to n terms ,there are two method you can use either use for loop or use formula Running time of using formula is Constant or 
Running time of using for loop is O(n) in words ' Order of N '

Method 1:- sum of series from 1 to N.

Formula =n(n+1)/2

Method 2:- sum of series from 1 to N.

for(i=1;i<=n;++i)
{
sum+=i;
}
If you are interested then you can modified this series for large number or you can try below series .

Method 1:- Using For Loop

#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;

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:-

Method 1:-
C++ Program To Find Sum Of The Following Series 1+2+3+4+5+6 . . . . . n






Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment