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

• November 3rd, 2013, 05:23 PM
JessicaCouture95
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!
• November 3rd, 2013, 05:31 PM
GregBrannon
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?
• November 3rd, 2013, 05:55 PM
JessicaCouture95
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.
• November 3rd, 2013, 06:04 PM
GregBrannon
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.
• November 3rd, 2013, 06:17 PM
JessicaCouture95
Re: for loop that calculates the total of a series of numbers
This is what I have so far:

Code java:

```  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);```
• November 3rd, 2013, 06:25 PM
Norm
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.
• November 3rd, 2013, 06:30 PM
JessicaCouture95
Re: for loop that calculates the total of a series of numbers
Just revised my code. Does this look good?

Code java:

```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); } }```
• November 3rd, 2013, 06:33 PM
Norm
Re: for loop that calculates the total of a series of numbers
Does it compile, execute and give the correct answer?
• November 3rd, 2013, 06:38 PM
JessicaCouture95
Re: for loop that calculates the total of a series of numbers
Just compiled and executed. It looks good, thank you!