Results 1 to 15 of 15
 10172012, 05:42 AM #1Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
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.
 10172012, 05:52 AM #2
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?
Sincerely, Joshua Green
Please REP if I help :)
 10182012, 04:05 AM #3Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
Re: Using loops to make fractions a decimals
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
 10182012, 04:14 AM #4
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?
Sincerely, Joshua Green
Please REP if I help :)
 10182012, 04:27 AM #5Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
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
Java Code:public class Prog166e { public static void main(String args[]) { int x = 2 ; int y = 2; for(x = 1 ; x< y; x++){ } } }
 10182012, 04:35 AM #6
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.
Java 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++; } }
Sincerely, Joshua Green
Please REP if I help :)
 10182012, 04:40 AM #7Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
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.
Last edited by MrPosotive; 10182012 at 04:42 AM.
 10182012, 04:49 AM #8Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
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...
Java 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++; } } }
Java Code:for(int x = 1; (x<y) && (y <=15) ;x++)
Java Code:if(x<=y; y++)
 10182012, 04:59 AM #9
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:
Java Code:for(int y = 2 ; etc...) for(int x = 1; etc...)
Sincerely, Joshua Green
Please REP if I help :)
 10182012, 05:12 AM #10Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
Re: Using loops to make fractions a decimals
Java 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++) } } }
 10182012, 05:20 AM #11
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.
Sincerely, Joshua Green
Please REP if I help :)
 10182012, 05:24 AM #12Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
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?
 10182012, 05:29 AM #13
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.
Sincerely, Joshua Green
Please REP if I help :)
 10182012, 05:55 AM #14Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
Re: Using loops to make fractions a decimals
Java 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); } } } }
 10182012, 06:15 AM #15Member
 Join Date
 Sep 2012
 Posts
 68
 Rep Power
 0
Similar Threads

How to reduce fractions in Java?
By uselessparadigm in forum New To JavaReplies: 9Last Post: 04092012, 09:02 PM 
how to make program that keeps score and loops till XXX is inputed
By jaki312 in forum New To JavaReplies: 2Last Post: 03132012, 09:07 PM 
trouble using fractions
By jeanOliver in forum New To JavaReplies: 2Last Post: 09062011, 09:48 PM 
Reducing fractions
By SwEeTAcTioN in forum New To JavaReplies: 10Last Post: 01132010, 05:51 PM 
NumberFormat which leave fractions as they are
By itaipee in forum AWT / SwingReplies: 2Last Post: 12092009, 07:11 PM
Bookmarks