Using loops to make fractions a decimals

Hey guys i got a program for class and i need to use loops to make fractions between 1 & 15 (the decimals so 1/2, 1/3, 2/3, 1/4 ect) and its also got to use the equivalent decimal beside them. I dont have any code yet because i really don't know where to start so could some one give tell me where to get started or point me in the right direction.

Re: Using loops to make fractions a decimals

Well, there are an infinite number of fractions, is there any more examples or information from the assignment?

Re: Using loops to make fractions a decimals

Quote:

Originally Posted by

**joshdgreen** Well, there are an infinite number of fractions, is there any more examples or information from the assignment?

Program 166e

(decimal equivalents

Program Description: Print a table of decimal equivalents of all proper fractions with denominators 2 through 15 inclusive with the numerator less than the denominator. Round the decimal to 5 places.

Statements Required: output, loop control

Sample Output:

1/2 0.50000

1/3 0.33333

2/3 0.66667

1/4 0.25000

2/4 0.50000

3/4 0.75000

1/5 0.20000

2/5 0.40000

3/5 0.60000

4/5 0.80000

1/6 0.16667

2/6 0.33333

3/6 0.50000

4/6 0.66667

5/6 0.83333

1/7 0.14286

2/7 0.28571

...more output here

5/14 0.35714

6/14 0.42857

7/14 0.50000

8/14 0.57143

9/14 0.64286

10/14 0.71429

11/14 0.78571

12/14 0.85714

13/14 0.92857

1/15 0.06667

2/15 0.13333

3/15 0.20000

4/15 0.26667

5/15 0.33333

6/15 0.40000

7/15 0.46667

8/15 0.53333

9/15 0.60000

10/15 0.66667

11/15 0.73333

12/15 0.80000

13/15 0.86667

14/15 0.93333

Thats the actual thing he gave us

Re: Using loops to make fractions a decimals

Okay, so looking at the fractions there is a pattern. The denominators increase with each number. There is 1 fraction with base 2, 2 fractions with base 3, 3 fractions with base 4, etc.... Along with this the numerator restarts at 1 each time that the denominator is increased. Does that help you get started at all?

Re: Using loops to make fractions a decimals

ok i think ive figured it out. i need to do a loop that makes the numerator go up untill it is 1 less then the denominator and i need to have the denominatior go up every time the first loop is done but im not sure how to do this yet Code:

`public class Prog166e {`

public static void main(String args[])

{

int x = 2 ;

int y = 2;

for(x = 1 ; x< y; x++){

}

}

}

theres the code

Re: Using loops to make fractions a decimals

You are on the right track. The code below outputs some fractions, maybe you can tweak it into doing what you would like. I used your code as the base for this example.

Code:

`public static void main(String args[])`

{

int y = 2;

for(int x = 1 ; (x < y) && (y <= 15); y++)

{

System.out.println(x + "/" + y);

x++;

}

}

Instead of incrementing x, I incremented y since that is what x is being compared to. Also you have to stop the y loop when y <= 15 so that is why I added the extra statement inside the for loop.

Re: Using loops to make fractions a decimals

yea that helps. Now ive just got to get it to convert to decimals lol, that will be fun. ive also got to get it to find as many possible numerators for the denominator.

Re: Using loops to make fractions a decimals

Ok i think ive figured out what i need to do but i dont know how to do it... Code:

`public class Prog166e {`

public static void main(String args[])

{

int y = 2;

for(int x = 1 ; (x < y) && (y <= 15); x++)

{

System.out.println(x + "/" + y);

y++;

}

}

}

heres the code i have now but what if i have the a for statment that says Code:

`for(int x = 1; (x<y) && (y <=15) ;x++)`

but we have a if statment inside of it that says something like that way it will go back to the for loop and ad 1 to y and then x will restart giving me 1/2,1/3,2/3 and so on. how would i code that though?

Re: Using loops to make fractions a decimals

Try two for loops, one for y and then the one for x inside that. Such as:

Code:

`for(int y = 2 ; etc...)`

for(int x = 1; etc...)

Re: Using loops to make fractions a decimals

Code:

`public class Prog166e {`

public static void main(String args[])

{

int y = 2;

for(y = 1 ; (x <= y); y++){

for(int x=1; x<= y; x++)

}

}

}

ok i did that but now i get the error "Syntax error on token ")", EmptyStatement expected after this token"

Re: Using loops to make fractions a decimals

First of all, didn't you want y to start at 2? You set it to 1 in the first for loop. Also, where to you compare y to being less than or equal to 15? You got rid of some of the correct coding that you had before.

Re: Using loops to make fractions a decimals

yea i didnt mean to get rid of that stuff it was an accident but yea do you know how to fix this error?

Re: Using loops to make fractions a decimals

Did you try putting code into the for loops? Maybe it is looking for code or brackets of some kind on the loops.

Re: Using loops to make fractions a decimals

Code:

`public class Prog166e {`

public static void main(String args[])

{

int y = 2;

int x = 1;

for(y = 2 ; (x <= y) && (y <=15); y++)

{

for( x=1; x< y; x++)

{

System.out.println(" ");

System.out.println(x +"/" + y);

}

}

}

}

Ok i got it doing the fractions correctly but now i dont know how to do the decimals.

Re: Using loops to make fractions a decimals

Ok i figured out thanks for all the help! + rep