+ 1

How to print this series

y=77-x/9!+x^2/8!-x^3/7!+×^4/6!-x^5/5!+x^6/4!-x^7/3!+x^8/2!-x^9/1! (^)means power

24th Feb 2017, 5:43 AM
Ali Zeawo
Ali Zeawo - avatar
4 Answers
+ 4
I wrote a loop to simplify your task. Define a function named 'fact' for calculating factorial and use this code. int x, y = 77; // Give 'x' a value. for (int loop = 1; loop < 10; loop++) y += Math.pow(-x, loop) / fact(10 - loop); System.out.println(y);
24th Feb 2017, 12:22 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 3
Get the value of x, either asking for user's input or as an input parameter. Then take into account + for addition, - for subtraction, * for multiplication, / for division and Math.pow() for the power operator. Finally, to compute the factorial operator, you may consider a library like Apache Commons Math ( http://commons.apache.org/proper/commons-math/userguide/utilities.html ), or code a factorial function on your own.
24th Feb 2017, 5:59 AM
Álvaro
+ 2
You'll need to make a method for the factorials (Google "Java factorial method") and pass the values to it. For the exponentiation you use Math.pow(num, exp). Then operator precedence should take care of the rest. Then it will look something like this: y=77-x/fact(9)+Math.pow(x, 2)/fact(8)-Math.pow(x, 3)/fact(7)+Math.pow(×, 4)/fact(6)-Math.pow(x, 5)/fact(5)+Math.pow(x, 6)/fact(4)-Math.pow(x, 7)/fact(3)+Math.pow(x, 8)/fact(2)-Math.pow(x, 9)/fact(1);
24th Feb 2017, 6:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
thanks
24th Feb 2017, 6:11 AM
Ali Zeawo
Ali Zeawo - avatar