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: for loop that calculates the total of a series of numbers

1. ## for loop that calculates the total of a series of numbers

Hi, I am having troubles with creating a for loop that calculates the total of a series of numbers: 1/30 + 2/29 + 3/28 + ... 30/1. I don't know where to start and need some help!

2. ## Re: for loop that calculates the total of a series of numbers

What about that series or programming it don't you understand . . . ?

Numerator starts at 1 and increases by 1 with each term. Denominator starts at 30 and decreases by 1 with each term. Pretty simple, but maybe it's hard for you to see as a program. What's hard to see?

3. ## Re: for loop that calculates the total of a series of numbers

I understand the series, and I understand what is being asked of me. But, I do not understand how to set it up in a for loop. I just learned for loops in my class, and I am not sure if this for loop needs 2 variables, given that there is a changing numerator and a denominator, and how to set up the for loop header.

4. ## Re: for loop that calculates the total of a series of numbers

You need to try and then post the effort with specific questions to get help. We can't write it for you. You could probably do something fancy with just 1 variable in the loop, but make it easier on your brain and use 2.

5. ## Re: for loop that calculates the total of a series of numbers

This is what I have so far:

```
for (int x = 1, y = 30; x <= 30, y >= 1; x++, y--)
{
int sum = 0;
sum += (x/y);
}

System.out.println("The sum of the series of numbers is " + sum);```

6. ## Re: for loop that calculates the total of a series of numbers

One problem could be the code is using int variables which will do int divisions:
9/10 = 0
Change the variable type to double to get better results.

7. ## The Following User Says Thank You to Norm For This Useful Post:

JessicaCouture95 (November 3rd, 2013)

8. ## Re: for loop that calculates the total of a series of numbers

Just revised my code. Does this look good?

```public class Count
{

public static void main (String[] args)
{
double sum = 0;

for (double x = 1, y = 30; x <= 30 && y >= 1; x++, y--)
{
sum += (x/y);
}

System.out.println("The sum of the series of numbers is " + sum);
}
}```

9. ## Re: for loop that calculates the total of a series of numbers

Does it compile, execute and give the correct answer?

10. ## The Following User Says Thank You to Norm For This Useful Post:

JessicaCouture95 (November 3rd, 2013)

11. ## Re: for loop that calculates the total of a series of numbers

Just compiled and executed. It looks good, thank you!