Thread: For Loops

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    For Loops


    Hello all, I'm stuck with a problem for my homework.

    Problem:

    Write a for loop that calculates the total of the following series of numbers:

    (1/30) + (2/29) + (3/28) + ... (30/1)

    So far I got this:

    int numerator, denomenator;

    for(int numerator = 1; int numerator <= 30; numerator++
    for(int denomerator = 30; int denomenator >= 1; denomenator--
    {
    System.out.println(numerator/denomenator);
    }

    I'm kind of lost on how to print out the fractions without outputing a decimal point. Thanks for the help!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Try using only one loop. Have a separate variable that starts at 30 and is decremented by 1 for the denominator.

    how to print out the fractions
    Build a String of the parts that you want to display vs having an arithmetic expression:
    3 + "/" + 4
    vs
    3/4
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    Are you just printing out the fractions themselves or you adding the fractions?

IMN logo majestic logo threadwatch logo seochat tools logo