A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

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

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

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

 Using Formula

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

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

total answers (2)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now