c# - Calculate 1 + 1!/X + 2!/X^2 + … + N!/X^N -
hi guys tried make program in c# calculates 1 + 1!/x + 2!/x^2 + … + n!/x^n giving wrong results tried debugging cant find mistake can me that?
console.writeline("calculate 1 + 1!/x + 2!/x^2 + … + n!/x^n"); console.write("n:"); double n = double.parse(console.readline()); console.write("x:"); double x = double.parse(console.readline()); double result = 1; int ifac = 1; (int = 1; <= n; i++) { (int j = i; j >= 1; j--) { ifac *= j; } result += ifac / math.pow(x, i); } console.writeline(result);
you need add ifac = 1;
in each loop.
console.writeline("calculate 1 + 1!/x + 2!/x^2 + … + n!/x^n"); console.write("n:"); double n = double.parse(console.readline()); console.write("x:"); double x = double.parse(console.readline()); double result = 1; int ifac = 1; (int = 1; <= n; i++) { ifac = 1; //this line important (int j = i; j >= 1; j--) { ifac *= j; } result += ifac / math.pow(x, i); } console.writeline(result);
Comments
Post a Comment