Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Loops & Series Question

1. ## Loops & Series Question

Assigned Question:

The infinite series:
" 1 + 22 * x + 32 * x2 + 42 * x 3 + ....."

Each term in the series is made up of a coefficient multiplying a power of x. The coefficients are the squares of the successive integers starting with 1, and the powers uses successive integers, starting with 1, and the powers uses successive exponents starting at 0. The sum converges when |x| is less than 1.

Write a program that reads x, verifies that |x| < 1 or else crashes, and then computes and outputs the sum together with the oracle's answer:
Oracle is: (1 + x) / (1 - x)3

To compute the sum, keep adding terms until you reach a term whose value is less than 0.0001 in absolute value.

I wrote the program.. but something is missing because when I compiled it & ran it... my program would keep printing the correct value ( the answer of the series of the sum) an infinite amount of times.. How do I fix it?

Here is my program:

```import java.io.PrintStream;
import java.util.Scanner;
import type.lib.ToolBox;

public class Check07C
{
public static void main(String[] args)
{
PrintStream out = System.out;
Scanner in = new Scanner(System.in);

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;  ;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)
{

out.println("The sum of the series = " + series);
}
else
{
series = series + term;
}
}

}
}
}```

2. ## Re: Loops & Series Question

Originally Posted by nuttaay
`          for (int i = 0;  ;i++)`
When do you expect that loop to exit?

3. ## 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:

```
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.

4. ## Re: Loops & Series Question

What do you mean by incorrect? How off is it?

5. ## Re: Loops & Series Question

COMMAND PROMPT:

Enter the value of x ... 0.001254