# [SOLVED] Trouble with this loop!

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-31-2009, 05:23 AM
PureAwesomeness
[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?
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);         } }```
• 01-31-2009, 05:32 AM
Fubarable
walk through your code in your mind or on paper as if you were the computer running it and you will see the problem.
• 01-31-2009, 06:06 AM
Eranga
Do you want to add just the above number series or any custom number series with the same pattern?
• 01-31-2009, 06:23 AM
PureAwesomeness
just the number series above.
• 01-31-2009, 06:35 AM
Eranga
So you want to simply added numbers, something like this.

Quote:

2/3 + 3/5 = 5/8
Seems to me in your code, trying to do that. Correct me if I'm wrong.
• 01-31-2009, 06:39 AM
PureAwesomeness
i need to add them through loops
• 01-31-2009, 06:40 AM
Fubarable
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.
• 01-31-2009, 06:45 AM
Eranga
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.
• 01-31-2009, 06:48 AM
Eranga
Quote:

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.

Quote:

2/3 + 3/5 = 5/8
• 01-31-2009, 06:51 AM
angryboy
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.
• 01-31-2009, 08:43 AM
aboylearning
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);
}
}
• 01-31-2009, 08:51 AM
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
• 01-31-2009, 08:52 AM
Fubarable
Quote:

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.
• 01-31-2009, 08:53 AM
Fubarable
Quote:

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.
• 01-31-2009, 08:53 AM
angryboy
oh wait, it won't work because the math is incorrect.
(1/3) + (3/5) != (1+3)/(3+5)
• 01-31-2009, 08:55 AM
Fubarable
Quote:

Originally Posted by angryboy
oh wait, it won't work because the math is incorrect.

as was stated above.... ;)
• 01-31-2009, 08:55 AM
angryboy
they really should add some features to this forum to show who's typing. we keep crossing paths ninja.
• 01-31-2009, 10:02 AM
PureAwesomeness
i want to make sure of something, is this mean that the program skip the even numbers?
if ( top%2==0)
continue;
• 01-31-2009, 10:30 AM
aboylearning
Quote:

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?
• 01-31-2009, 10:32 AM
aboylearning
Quote:

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