Print following series 1 + 2x + 3x^2 + 4x^3...............(n+1)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;
int power2;
power2=power+1; // for manage the loop iteration
int ans=1;
int ans2=1;
int sum=0;
int i=1;
while (i<=power2) // outer loop for series
{
for (int j=1;j<=i;j++) // inner loop for make power
{
if (j<=1) // for manage the series and write only 1
ans=1;
else if (j<=2) // for second factor then series and for not print power on second digit
ans=base;
else
ans=(ans*base); // for power
}
if (i==1) // for not multiply coffient with base in first digit
ans2=ans;
else
ans2=i*ans; // for multiply the cofficient with base
sum=sum+ans2;
ans=1;
ans2=1;
i++;
}
cout<<"The sum of the following 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;
int power2;
power2=power+1; // for manage the loop iteration
int ans=1;
int ans2=1;
int sum=0;
int i=1;
while (i<=power2) // outer loop for series
{
for (int j=1;j<=i;j++) // inner loop for make power
{
if (j<=1) // for manage the series and write only 1
ans=1;
else if (j<=2) // for second factor then series and for not print power on second digit
ans=base;
else
ans=(ans*base); // for power
}
if (i==1) // for not multiply coffient with base in first digit
ans2=ans;
else
ans2=i*ans; // for multiply the cofficient with base
sum=sum+ans2;
ans=1;
ans2=1;
i++;
}
cout<<"The sum of the following series is:"<<sum<<endl;
}
Comments
Post a Comment