e^x function

hi plz help with my homework,I have to write a program that gave me an answer:
x-((x^3)/3!)+((x^5)/5!)-...
here's my code. my actual problem is idk how to write 1!,3!,5!,7!,...



#include <cmath>
#include <iostream>
using namespace std;
int main(){
int b,a=0,c,g=1,f=1,x,n=0;
double i,sum=0;
cout<<"x=";
cin>>x;
cout<<"a=";
cin>>a;

while (n<a)
{


b=x*(pow((double) -1,n));
c=pow((double) b,2*n+1);

d=(n+1) * n * g;
g *= d;

i= (1.*c)/g;



(double) sum +=i;

n++;
}
cout<<"sum="<<sum+1<<endl;

return 0;
}
Last edited on
You're incrementing n by 2 each time, but your code is written assuming n is incremented by 1 each time.
Topic archived. No new replies allowed.