Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36
  1. #21
    aboylearning is offline Member
    Join Date
    Jan 2009
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by PureAwesomeness View Post
    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. #22
    aboylearning is offline Member
    Join Date
    Jan 2009
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by PureAwesomeness View Post
    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. #23
    floydg is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default

    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. #24
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

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

  5. #25
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    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]);
    }
    instead of
    total = total + (num / den);
    Last edited by Webuser; 02-01-2009 at 01:10 AM.

  6. #26
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    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 01:09 AM.

  7. #27
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    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 04:25 AM.

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

    Default

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

    Default

    you don't need "total = 0;" inside the for loop.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #30
    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
    you don't need "total = 0;" inside the for loop.
    D'oh! and that too!

  11. #31
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    last time didn't put that in, and my program wasn't able recongize that. =[

  12. #32
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Thankyou people! i got it.

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

    Default

    if the post is solved, please mark it as solved. and may we see some codes??
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #34
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Yeah, paste your code version here :)

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

    Default

    a very big mistake, you are making total= 0 in every loop??????????????????

  16. #36
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by aboylearning View Post
    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 FirstFirst 12

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
  •