using recrsion.

write a program that includes a recursive function to evalute the first n terms in the series specified by y = 1 - x + (x^2)/2 - (x^3)/6 + (x^4)/24 + ...... + (-1)^n (x^n)/n! where n(defined as int ) and x ( defined as double ) are input parameters


here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

double se( int n , double x );
int main ()
{
 int n, x;
 cout << "enter an integer and a number of type double: " << endl;
 cin >> n >> x;

 cout << "the first " << n << "terms: " << se( n, x );
 
 return 0;
}

double se( int n , double x )
{
 if ( n == 0 )
  return 1;
 else 
  
}




i only need to know what is the recersion step
so can u help me with that
Last edited on
Hint 1:
You know that for the nth term the term's value is always:

(-1^n)(x^n)/n!

You also know that if n is zero that the answer is always 1.

So, what do you have to do to n each time you calculate the next term? And how do you know when to stop?

Hint 2:
Add backwards.

Hope this helps.
Last edited on
you can use recursivity to calclate the factorial:
1
2
3
4
5
6
double factorial(double x)
{if (x>1)
  return(x*factorial(x-1));
else 
  return 1;
}


Topic archived. No new replies allowed.