Re: Loops & Series Question
Quote:
Originally Posted by
nuttaay
When do you expect that loop to exit?
Re: Loops & Series Question
I want it to exit the loop when a term's absolute value is less than 0.0001. Thus I added a condition in the for loop:
Code Java:
boolean check = true;
out.print("Enter the value of x ... ");
double x = in.nextDouble();
double series = 0;
if (Math.abs(x) > 1.0)
{
ToolBox.crash(true, "|x| must be < 1.");
}
else
{
double y = Math.pow(1 - x, 3);
double oracle = (1 + x) / y;
out.println("The Oracle's answer is: " + oracle);
for (int i = 0; check == true; i++)
{
double pow = Math.pow(i, 2);
double pow1 = Math.pow(x, i - 1.0);
double term = pow * pow1;
if (Math.abs(term) < 0.0001)
{
check = false;
series = series + term;
out.println("The sum of the series = " + series);
}
else
{
series = series + term;
}
}
}
}
}
but now, the answer to the sum of the series is incorrect. :(
Re: Loops & Series Question
What do you mean by incorrect? How off is it?
Re: Loops & Series Question
COMMAND PROMPT:
Enter the value of x ... 0.001254
The Oracle\'s answer is: "1.005030184256893"
The sum of the series = 0.0
From my textbook, it says the answer for the sum of the series should be : 1.005016
Re: Loops & Series Question