Print and calculate the sum of following series x + x^2 + x^3 + x^4.................x^n
#include <iostream>
using namespace std;
void main ()
{
cout<<"Please enter the Base (X) here:";
int base;
cin>>base;
cout<<"Please enter the power (n) here: ";
int power;
cin>>power;
cout<<"\n"; // for line space only
int i=1; //outer loop
int ans=1;
int sum=0;
while (i<=power)
{
cout<<base<<"^"<<i<<" ";
if (i!=power) // for control the '+' sign
cout<<" + ";
else
; // black statement
for (int j=1; j<=i;j++)
{
ans=ans*base; //base multiply it self by number od power e.g. 2^3= 2*2*2 = 8
}
sum=sum+ans;
ans=1; // for remove the old value
i++; // increament of the outer loop
}
cout<<"\n\nThe sum of the above series is:"<<sum<<endl;
}
using namespace std;
void main ()
{
cout<<"Please enter the Base (X) here:";
int base;
cin>>base;
cout<<"Please enter the power (n) here: ";
int power;
cin>>power;
cout<<"\n"; // for line space only
int i=1; //outer loop
int ans=1;
int sum=0;
while (i<=power)
{
cout<<base<<"^"<<i<<" ";
if (i!=power) // for control the '+' sign
cout<<" + ";
else
; // black statement
for (int j=1; j<=i;j++)
{
ans=ans*base; //base multiply it self by number od power e.g. 2^3= 2*2*2 = 8
}
sum=sum+ans;
ans=1; // for remove the old value
i++; // increament of the outer loop
}
cout<<"\n\nThe sum of the above series is:"<<sum<<endl;
}
Comments
Post a Comment