Reputation: 3
I was assigned an assignment where I have to create a program to add the following numbers and then tell the user the answer while using a loop statement.
(1/3 + 3/5 + 5/7 + 7/9 + 9/11 + 11/13 + ... 95 / 97 + 97/99) The answer should be 45.12445 according to the professor but I am receiving the answer 40.92541 and I'm having trouble figuring out where the error in my logic is.
public class HW6
public static void main(String[]args)
{
//Declarations
double num;
double denom;
double holder1;
double sum;
num=1;
denom = 3;
sum=0;
// Perform loop
for (num=1;num<=98;num=num+2) {
num = num+2;
denom = denom+2;
holder1 = num/denom;
sum= sum+holder1;
}
// Print sum
System.out.println("The sum is " +sum);
}
}
Upvotes: 0
Views: 2889
Reputation: 3112
To understand the problem in your logic, take it one step at a time. Your initialization before the loop looks fine, so let's start at the beginning of the loop:
Iteration 1
for (num=1;num<=98;num=num+2) {
At this point, your variables contain the following:
num == 1, denom == 3, sum == 0, and holder == undefined (because you didn't initialize it)
After the following statement:
num = num+2;
num
changes to 3, so we now have:
num == 3, denom == 3, sum == 0, and holder == undefined.
After the next statement:
denom = denom+2;
denom
changes from 3 to 5, so we have:
num == 3, denom == 5, sum == 0, and holder == undefined.
Then, after
holder1 = num/denom;
we have the first calculated fraction, which (since num==3 and denom==5) is 3/5, or 0.6. You will probably notice that this is not what you intended, because according to your assignment the first fraction in the series is supposed to be 1/3. So the first problem with your logic is that you are adding 2 to both num
and denom
before you calculate the first fraction in the series.
Continuing to step through your code, after
sum= sum+holder1;
the variables contain
num == 3, denom == 5, sum == 0.6, and holder == 0.6.
Iteration 2
Now before looping back to execute the next iteration of the for
loop, the num
variable will be incremented by 2, as specified by the third expression in your for
loop definition: num=num+2
. So when the loop begins for the second iteration, the variables contain:
num == 5, denom == 5, sum == 0.6, and holder == 0.6.
As before, the next two statements increment num
and denom
, respectively, by 2, so before the next fraction is computed, the variables contain:
num == 7, denom == 7, sum == 0.6, and holder == 0.6.
Variable holder
is then computed as num/denom
= 7/7 = 1.0 (which is certainly not a fraction that is part of the correct series), and after adding this to sum
, you have
num == 7, denom == 7, sum == 1.6, and holder == 1.0.
Conclusion
So there are at least two errors in your logic. The first is that after initializing num
and denom
to 1 and 3, respectively, you add 2 to each before calculating their ratio. What you probably should do instead is add 2 to each of num
and denom
after you calculate their ratio, but before jumping back to the start of the loop.
The second error, which you hopefully noticed as we stepped through the code, is that you are adding 2 to num
twice with each time through the loop: once at the bottom of the loop according to your for
specifier, and once at the top of the loop. You only need one of those increment-by-2 operations.
So an improved loop would look like:
for (num=1;num<=98;num=num+2) {
holder1 = num/denom;
sum= sum+holder1;
denom = denom+2;
}
Upvotes: 1