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!
Printable View
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!
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?
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.
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.
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);
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.
Does it compile, execute and give the correct answer?
Just compiled and executed. It looks good, thank you!