e^x= 1+(X/1!)+(x^2/2!)+(x^3/3!)....

hi
i do my homework.
but i cant write it in class ... please help:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=1;
int counter =1;
int total=1;
int s=1;
int m=1;
int x;
cout<<"enter value of x :";
cin>>x;
while(counter <= x)
{
while( a<=counter)
{
s*=x;
m*=counter;
a++;}
total+=(s/m);
counter++;
}
cout<<"total = : "<<total;
getch();
return 0;
}

1
2
3
4
5
unsigned limit = 7;// do 7 iteration by default
while(counter <= limit) {
    total += power(x, 2)/fact(counter);
    counter++;
}

Then specify functions power() and fact()
Last edited on
Several different issues here. First, the important variables used in the calculation need to be of type double, not int.

Since this is the summation of an infinite series, we need some way to determine how many terms to include. One way is to ask the user to enter that value. The more terms we use, the more accurate result - but beyond a certain point the later terms become too small to make a difference.

And there is a logic error, only a single loop is required, to iterate through each of the terms of the series.
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
33
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int counter=1;
    int numterms;

    double total=1;
    double s=1;
    double m=1;
    double x;

    cout << "enter value of x : ";
    cin >> x;
    cout << "enter number of terms : ";
    cin >> numterms;

    while (counter <= numterms)
    {
        s *= x;
        m *= counter;
        total += (s/m);
        counter++;
    }

    cout << "total = " << total;

    getch();
    return 0;
}


Output:
enter value of x : 1
enter number of terms : 10
total = 2.71828
Last edited on
wow yesss. thanks so much...now i understand my mistakes. thanks miiinipaa
and thanks so so so chervil
Topic archived. No new replies allowed.