- 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

12th Sep 2017, 7:31 AM
Koolest
Koolest - avatar
5 Answers
+ 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; }
12th Sep 2017, 7:36 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
edited
12th Sep 2017, 7:46 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
factorial is not a sum @Tony
12th Sep 2017, 8:30 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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 }
15th Sep 2017, 10:06 PM
Mikhael Anthony
Mikhael Anthony - avatar
0
in the simplest manner
12th Sep 2017, 7:38 AM
Koolest
Koolest - avatar