Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Adding fractions

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Adding fractions

    Hello everyone, this should be an easy problem for anyone her I believe because I'm just not noticing why it won't display the answer. I'm trying to solve this book problem......
    "Write a for loop that calculates the total of the follower series of numbers:
    1/30 + 2/29 + 3/28......+30/1"

    Here is what I have..
    public static void main(String[] args) {
    double total = 0;
    for (double a = 1, b = 30; b < 1; a++, b--) {
    total += (a / b);
    }
    System.out.println(total);

    }
    }
    When launched, the output is 0.0. I tried changing the variables a and b to doubles but didn't change anything, any help? Thanks!

    P.s how do you post a code correctly on this forum?
    Last edited by freefora11; March 8th, 2014 at 06:44 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Adding fractions

    Show the version with doubles. Read about 'integer math'.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Adding fractions

    Hi,

    To post code in the forum, simply wrap the code with [code] tags, e.g.,

    [code]Your
    code
    here[/code].

    The problem is in your 'for' loop:

    for (double a = 1, b = 30; b < 1; a++, b--) {

    The parts of a 'for' loop:

    for (initialization; termination; increment)

    When the termination expression evaluates to false, the loop terminates. Your termination expression is b < 1, whereas your initialisation expression for b is b = 30.

    If you still don't see the problem, try adding

    System.out.println("a = " + a);
    System.out.println("b = " + b);

    to the body of your 'for' loop.

  4. The Following User Says Thank You to jashburn For This Useful Post:

    freefora11 (March 8th, 2014)

  5. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Adding fractions

    Oh duh, for some reason I thought when its true it terminates, thanks for the refresher, I just changed it to b > 1. Thanks

Similar Threads

  1. Removing fractions from Integers
    By Blinktwink in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 27th, 2013, 01:18 AM
  2. Array, fractions, etc
    By Kristenw17 in forum Collections and Generics
    Replies: 1
    Last Post: April 17th, 2013, 08:29 PM
  3. Fractions
    By ps3lover3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 29th, 2013, 09:10 PM
  4. Fractions Code help
    By SOK in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 9th, 2010, 11:07 AM
  5. Fractions
    By debug in forum Java Theory & Questions
    Replies: 1
    Last Post: March 8th, 2010, 11:29 PM