Page 1 of 2 12 LastLast
Results 1 to 20 of 36
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default [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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

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

  4. #4
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    just the number series above.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #6
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    i need to add them through loops

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by PureAwesomeness View Post
    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. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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 06:00 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    aboylearning is offline Member
    Join Date
    Jan 2009
    Posts
    16
    Rep Power
    0

    Default

    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. #12
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aboylearning View Post
    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. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by angryboy View Post
    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. #15
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    oh wait, it won't work because the math is incorrect.
    (1/3) + (3/5) != (1+3)/(3+5)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

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

  17. #17
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    they really should add some features to this forum to show who's typing. we keep crossing paths ninja.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  18. #18
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    i want to make sure of something, is this mean that the program skip the even numbers?
    if ( top%2==0)
    continue;

  19. #19
    aboylearning is offline Member
    Join Date
    Jan 2009
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by angryboy View Post
    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 09:36 AM.

  20. #20
    aboylearning is offline Member
    Join Date
    Jan 2009
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by angryboy View Post
    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 LastLast

Similar Threads

  1. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 02:18 AM
  2. Trouble with method
    By BlueJ2008 in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 09:05 PM
  3. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM
  4. compiling trouble
    By capacitator in forum CLDC and MIDP
    Replies: 4
    Last Post: 06-10-2008, 10:12 PM
  5. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 04:25 AM

Posting Permissions

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