Find sum of series 1+x+x^2+……+x^n – C++ program

CWC
2 Min Read

Write C++ program to find sum of series 1+x+x^2+……+x^n

#include
#include
#include
using namespace std;
int main()
{
long i,n,x,sum=1;
cout<<"1+x+x^2+......+x^n";
cout<<"\n\nEnter the value of x and n:"; cin>>x>>n;
for(i=1;i<=n;++i)
sum+=pow(x,i);
cout<<"\nSum="<<sum;
return 0;
}

C++ Program To Find Sum Of Series 1+x+x^2+……+x^n

The below program is written to find the sum of the above series.


#include

using namespace std;

int main()

{

int n,a,s=0,sum;

cout<<"Enter the value of n:";

cin>>n;

for(a=1;a<=n;a++)

{

s=s+a;

}

cout<<"The sum of series is:"<<s;< p=""></s;<>

return 0;

}


#include

using namespace std;

int main()

{

int n,a,s=0,sum;

cout<<"Enter the value of n:";

cin>>n;

for(a=1;a<=n;a++)

{

s=s+a;

}

cout<<"The sum of series is:"<<s;< p=""></s;<>

return 0;

}

C++ Program to find Average Of Multiple Numbers

Average of multiple numbers=A

If you have to find the average of multiple numbers, then you need to use a formula. The formula is, average of the first number + (average of the second number) + (average of the third number) +…+(average of the last number).

The question is asked very often in my class, I have written this program to find sum of series 1+x+x^2+……+x^n, i have to use recursion method but it is taking too much time to run. Please check and give me solution in better way as i am new in C++ programming.

C++ program to find sum of series 1+x+x^2+……+x^n:Code


#include
using namespace std;
int main()
{
long int n,i,sum=0,j,k,l,a[100];
cout<<"Enter number of terms"<<endl;< p=""></endl;<>
cin>>n;
for(i=1;i<=n;i++)
{
j=i;
while(j>0)
{
k=j%10;
l=j/10;
a[k]=a[k]+1;
j=j/10;
}
sum=sum+a[i];
}
cout<<"Sum of series is"<<sum<<endl;< p=""></sum<<endl;<>
return 0;
}

Enter number of terms

5

Sum of series is 40

Share This Article
Leave a comment

Leave a Reply

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

English
Exit mobile version