# Thread: [SOLVED] Trouble with this loop!

1. Member
Join Date
Jan 2009
Posts
90
Rep Power
0

## [SOLVED] Trouble with this loop!

i want to write a program to sum of this serie 1/3 + 3/5 + 5/7 + ...+ 95/97.
in my program what i did is that make a loop for the numerator and denominator, both will skip even number. then i put the num and den together and add them to get the sum, but some how it gives me 0.0.

any suggestions for what i need to change?
Java Code:
```public class TT{
public static void main (String[]args){

System.out.println("what is the sum if this serie: 1/3 + 3/5 + 5/7 + ...+ 95/97 + 97/99?");
int a = 1;
int b = 3;
double total = 0;
int num=0;
int den=0;
for (int top = a; top<=97; top++){

if ( top%2==0)
continue;
num = top;
}
for (int bottom = b; bottom<= 99; bottom++){

if (bottom%2 == 0)
continue;
den = bottom;

}

total = total + (num / den);

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

2. walk through your code in your mind or on paper as if you were the computer running it and you will see the problem.

3. Do you want to add just the above number series or any custom number series with the same pattern?

4. Member
Join Date
Jan 2009
Posts
90
Rep Power
0
just the number series above.

5. So you want to simply added numbers, something like this.

2/3 + 3/5 = 5/8
Seems to me in your code, trying to do that. Correct me if I'm wrong.

6. Member
Join Date
Jan 2009
Posts
90
Rep Power
0
i need to add them through loops

7. nah, he does one loop and discards the numbers from the loop except the last, does another loop, discards the numbers from the loop except the last, then uses the last numbers from both loops. Not good.

8. Yes Fubarable, that's why I'm mess up. His code is doing unusual thing. That's not the correct way to find the summation of those.

9. Originally Posted by PureAwesomeness
i need to add them through loops
Yes, you have to use a loop. But to use a loop you must able to iterate one by one numbers at a time. So best thing is place all numbers in an array. Then, according to my 2nd post here, how you going to add them. Just adding values?

In maths the following is wrong.

2/3 + 3/5 = 5/8

10. my suggestion is to use 3 variables. sum, num, den.
then put them in a for loop that increments by 2.
sum += (num+n)/(den+n);

like fubarable pointed out, work out the math on paper first. then the rest is easy.
Last edited by angryboy; 01-31-2009 at 07:00 AM.

11. Member
Join Date
Jan 2009
Posts
16
Rep Power
0
Hi check this code, i think it will solve your problem.

------------------------------------------------------
public class TT{
public static void main (String[]args){

System.out.println("what is the sum if this serie: 1/3 + 3/5 + 5/7 + ...+ 95/97 + 97/99?");
int a = 1;
int b = 3;
double total = 0;
int num=0;
int den=0;

for (int top = a; top<=97; ){

num += top;
top++;
top++;

}
for (int bottom = b; bottom<= 99; ){

den += bottom;
bottom++;
bottom++;

}

total = total + (num / den);

System.out.println("sum of the series is: "+ total);
}
}

12. oh man, if chris was here, he'd prob chew you up. haha. we try not to give away the ans here. but rather let the OP learn it with our guidance... welcome to java forums

13. Originally Posted by aboylearning
Hi check this code, i think it will solve your problem.
Your enthusiasm to help is appreciated, but if you're not sure of the solution, it's probably better to remain silent rather than to mislead a newbie.

Best of luck.

14. Originally Posted by angryboy
oh man, if chris was here, he'd prob chew you up. haha. we try not to give away the ans here. but rather let the OP learn it with our guidance... welcome to java forums
even worse than giving "the answer" in my book is giving a patently wrong answer as was done above.

15. oh wait, it won't work because the math is incorrect.
(1/3) + (3/5) != (1+3)/(3+5)

16. Originally Posted by angryboy
oh wait, it won't work because the math is incorrect.
as was stated above.... ;)

17. they really should add some features to this forum to show who's typing. we keep crossing paths ninja.

18. Member
Join Date
Jan 2009
Posts
90
Rep Power
0
i want to make sure of something, is this mean that the program skip the even numbers?
if ( top%2==0)
continue;

19. Member
Join Date
Jan 2009
Posts
16
Rep Power
0
Originally Posted by angryboy
oh man, if chris was here, he'd prob chew you up. haha. we try not to give away the ans here. but rather let the OP learn it with our guidance... welcome to java forums
Hi, but man i think he know "how to program" but he is finding it hard to make logic of this particular program, and the reason why people are not able to answer that question because he has make things confusing , either sum only dom and rem and then add it to variable sum or simple add consecutive nos.

Plus what is the purpose of wasting that guy time if we can help to figure out in correct program instead of wasting his time in things...

i dont know the policy of this forum just make an account but i believe if you know the right answer you should post it right?
Last edited by aboylearning; 01-31-2009 at 10:36 AM.

20. Member
Join Date
Jan 2009
Posts
16
Rep Power
0
Originally Posted by angryboy
oh wait, it won't work because the math is incorrect.
(1/3) + (3/5) != (1+3)/(3+5)
I think i knew this much math :D but as Eranga has concluded may be this guy is looking for this kind of solution, so it is doing that thing.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•