- 1
Can anyone give c++ code for- 1+x+x^2/2!+x^3/3!.......x^n/n!
please do it in a simple manner.Thanx
5 Respuestas
+ 1
double sum = 1;
unsigned power = 1, fact = 1;
for(unsigned i = 0; i < n; ++i)
sum += (power *= x) / 1.*(fact *= i + 1);
cout << sum << endl;
Edit : easier way to read
for(unsigned i = 1; i <= n; ++i){
power *= x;
fact *= i;
sum += 1.0 * power / fact;
}
+ 1
edited
+ 1
factorial is not a sum @Tony
+ 1
lemme guess :
int n = 20; //loop limit
int x = 2; //you decide constant x
int y = 1; //for factorial
int z = 0; //will be your result
for( int i = 1 ; i < n ; i++ ) { //increment for ^
y = 1; //init y back to 1 everytime factorial , y is factorial
for( int j = i ; j > 0 ; j-- ) y = y * j; //factorial here
z = z + ( pow( x , i ) / y ); //the z calculation
}
0
in the simplest manner