Sum of the series: x +x^2/3!+x^3/5!+...

The result I am getting is wrong. What's wrong in my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream.h>
#include<math.h>
double SUMFUN(double,int);
void main()
{double y;
 int m;
 cout << "Enter the value of X: ";
 cin >> y;
 cout << "\nEnter the value of n: ";
 cin >> m;
 SUMFUN(y,m);
 cout << "\nThe sum of the series X + X^2/3! + X^3/5! + X^4/7!.... upto " << m << " terms is: " << SUMFUN(y,m);
}

double SUMFUN(double X, int n)
{ double res,temp,div;
  int fact=1;

{ for(int i=1;i<=n;i++)
  fact=1;
  if(i!=1)
{ fact=1;
  for(int k=1;k<=2*i-1;k++)
  fact=fact*k;
}

  temp=pow(X,i);
  div=temp/fact;
  res+=div;
}
return(res);
}
What you have there is analogous to:
1
2
3
4
5
6
int foo ( int bar )
{
  int gaz;
  gaz += bar;
  return gaz;  // What is the value of gaz now?
}



PS. Your code indentation could be better.
Topic archived. No new replies allowed.