# [SOLVED] Trouble with this loop!

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• 01-31-2009, 10:44 AM
aboylearning
Quote:

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 = ...
...........
• 01-31-2009, 10:53 AM
aboylearning
Quote:

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.
• 01-31-2009, 02:16 PM
floydg
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
• 02-01-2009, 01:49 AM
Webuser
Quote:

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 :(
Report me if that gonna help you :)
• 02-01-2009, 02:02 AM
Webuser
Quote:

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:
Quote:

num = top;
you may use
Quote:

index++;
num [index]= top;
Think it...
Because to get a real totoal sum you may use code like a
Quote:

for(int i=0; i<index; i++){
total = total + (num[i] / den[i]);
}
Quote:

total = total + (num / den);
• 02-01-2009, 02:06 AM
Webuser
It seems that the code you wrote will give
Quote:

97/99
as a total result. Check it out.
That's why I propose to use an array or a vector instead :)
• 02-02-2009, 05:13 AM
PureAwesomeness
i modify the program i posted, and i think this should work. this program still gives me 0.0. after aboylearning's suggestion
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);
}
}

• 02-02-2009, 05:16 AM
Fubarable
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:

Code:

double fraction = (double)intVarA / intVarB;
• 02-02-2009, 05:16 AM
angryboy
you don't need "total = 0;" inside the for loop.
• 02-02-2009, 05:18 AM
Fubarable
Quote:

Originally Posted by angryboy
you don't need "total = 0;" inside the for loop.

D'oh! and that too!
• 02-02-2009, 05:27 AM
PureAwesomeness
last time didn't put that in, and my program wasn't able recongize that. =[
• 02-02-2009, 05:32 AM
PureAwesomeness
Thankyou people! i got it.
• 02-02-2009, 05:45 AM
angryboy
if the post is solved, please mark it as solved. and may we see some codes??
• 02-02-2009, 05:49 AM
Webuser
Yeah, paste your code version here :)
• 02-02-2009, 07:03 AM
aboylearning
a very big mistake, you are making total= 0 in every loop??????????????????
• 02-02-2009, 08:04 PM
Webuser
Quote:

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 :(
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12