# Thread: [SOLVED] Trouble with this loop!

1. Member
Join Date
Jan 2009
Posts
16
Rep Power
0
Originally Posted by PureAwesomeness
i want to make sure of something, is this mean that the program skip the even numbers?
if ( top%2==0)
continue;
yes it will skip the even nos but the way i skip it is little different method but it will also skip even nos.

Are looking for such kind of solution?

1/3 + 3/5 = 14/15
14/15 + 5/7 = ...
...........

2. Member
Join Date
Jan 2009
Posts
16
Rep Power
0
Originally Posted by PureAwesomeness
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?

It is giving 0.0 because your den will always be greater than num, i hope you will get this point and will know where to go from here.

3. Member
Join Date
Jan 2009
Posts
1
Rep Power
0
Some simple hints to devise the algorithm:

1. The denominator is always two more than the numerator. You may be able to accomplish the iteration with only one loop.

2. The numerator increases by two's. The "for" loop can also increment by two's by using "x += 2" instead of "x++" and then checking a remainder inside the loop.

Floyd

4. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
if ( top%2==0) continue;
num = top;
write if and continue statements in one line
The 0.0 result may happen because it is always continue; statement in your code activated :(

5. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
1/3 + 3/5 + 5/7 + ...+ 95/97 + 97/99?");
The way you use to get the result is not right :(
You have to use an array instead of:
num = top;
you may use
index++;
num [index]= top;
Think it...
Because to get a real totoal sum you may use code like a
for(int i=0; i<index; i++){
total = total + (num[i] / den[i]);
}
total = total + (num / den);
Last edited by Webuser; 02-01-2009 at 02:10 AM.

6. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
It seems that the code you wrote will give
97/99
as a total result. Check it out.
That's why I propose to use an array or a vector instead :)
Last edited by Webuser; 02-01-2009 at 02:09 AM.

7. Member
Join Date
Jan 2009
Posts
90
Rep Power
0
i modify the program i posted, and i think this should work. this program still gives me 0.0. after aboylearning's suggestion
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;

double total = 0;

for (int top = a; top<=97; top += 2){
total = 0;
total += top/(2+top);

}

System.out.println("sum of the series is: "+ total);
}
}
Last edited by PureAwesomeness; 02-02-2009 at 05:25 AM.

8. Now you need to read up on int addition. Understand that an int over an int will result in an int, rounded to nearest int at or below the result. So 4/5 will result in 0, 98/99 will result in 0, 999998 / 999999 will result in 0. If you add 0 and 0 and 0, you get,... 0.

So use doubles here:

Java Code:
double fraction = (double)intVarA / intVarB;

9. you don't need "total = 0;" inside the for loop.

10. Originally Posted by angryboy
you don't need "total = 0;" inside the for loop.
D'oh! and that too!

11. Member
Join Date
Jan 2009
Posts
90
Rep Power
0
last time didn't put that in, and my program wasn't able recongize that. =[

12. Member
Join Date
Jan 2009
Posts
90
Rep Power
0
Thankyou people! i got it.

13. if the post is solved, please mark it as solved. and may we see some codes??

14. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Yeah, paste your code version here :)

15. Member
Join Date
Jan 2009
Posts
16
Rep Power
0
a very big mistake, you are making total= 0 in every loop??????????????????

16. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Originally Posted by aboylearning
a very big mistake, you are making total= 0 in every loop??????????????????
I think that too. It is because of the continue; wrong location :(

Page 2 of 2 First 12

#### Posting Permissions

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