math sum problem
i m trying to solve the following equation:
1-x^2/2 + x^3/3 - x^4/4 + x^5/5 + ....x^n/n
where value of x and n are obtained by the user.
I made the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include<iostream.h>
#include<math.h>
void main()
{
int total=1;
double x;int n;
cout<<"enter value of x";
cin>>x;
cout<<"enter value of n";
cin>>n;
for (int i=2;i<=n;i++)
{ if(i%2!=0)
total+=double pow(double x,int i)/i;
else total-=double pow(double x,int i)/i;
}
cout<<"\n"<<total;
}
|
total should be type double
, not int.
This line: total+=double pow(double x,int i)/i;
should read: total += pow(x, i)/i;
total+=double pow(double x,int i)/i;
→→ total+=pow(x, i)/i;
Read more about function calling.
Topic archived. No new replies allowed.