0

Hi,

I programmed this code to solve the equation:

x/2! + x^n-1/n! where n is the size of series

The program functions, but appears to be giving me the incorrect result.

For example if I enter 3 for n, and 2 for x I am expecting 1.67 as the result but am getting 1.36

Could you pleae take a look at my for loops where the solution is being calculated and see if my logic there is wrong, and if it is, could you please tell me what to fix.

I've tried removing the inner loop and making the outer loop instead:

```
for (int i = 2; i <= nth; i++) {
solution += ((Math.pow(x, i-1)) / factorial(2));
}
```

but that when n = 3, and x = 2 gives a result of 2 which is also wrong

```
import java.util.Scanner;
import java.text.DecimalFormat;
public class SolveEquation {
static int factorial = 1;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.##");
double solution = 0;
System.out.println("Solve the series x/2! + x^2/3! + x^3/4! ... x^nth-1/nth!");
System.out.println("Enter the nth value");
int nth = input.nextInt();
System.out.println("Enter a value for x");
double x = input.nextDouble();
for (int i = 1; i < nth; i++) {
for (int j = 2; j <= nth; j++) {
solution += ((Math.pow(x, i)) / factorial(j));
}
}
System.out.println(df.format(solution));
}
public static int factorial(int n) {
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
}
```