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!
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
Members have full access to the forums. Advertisements are removed for registered users.
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:
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.
If you don't understand my answer, don't ignore it, ask a question.
JessicaCouture95 (November 3rd, 2013)
Does it compile, execute and give the correct answer?
If you don't understand my answer, don't ignore it, ask a question.
JessicaCouture95 (November 3rd, 2013)
Just compiled and executed. It looks good, thank you!