Results 1 to 15 of 15
  1. #1
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

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

  3. #3
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Using loops to make fractions a decimals

    Quote Originally Posted by joshdgreen View Post
    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

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

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

  5. #5
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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++){
    			
    		}
    	}
    }
    theres the code

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default 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++;
            }
        }
    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.
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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; 10-18-2012 at 03:42 AM.

  8. #8
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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++;
            }
            
        }
    }
    heres the code i have now but what if i have the a for statment that says
    Java Code:
    for(int x = 1; (x<y) && (y <=15) ;x++)
    but we have a if statment inside of it that says something like
    Java Code:
    if(x<=y; y++)
    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?

  9. #9
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

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

  10. #10
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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++)
            }
        }
    }
    ok i did that but now i get the error "Syntax error on token ")", EmptyStatement expected after this token"

  11. #11
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

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

  12. #12
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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?

  13. #13
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

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

  14. #14
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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);
            	}
            		
            }
        }
    }
    Ok i got it doing the fractions correctly but now i dont know how to do the decimals.

  15. #15
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Using loops to make fractions a decimals

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

Similar Threads

  1. How to reduce fractions in Java?
    By uselessparadigm in forum New To Java
    Replies: 9
    Last Post: 04-09-2012, 08:02 PM
  2. Replies: 2
    Last Post: 03-13-2012, 08:07 PM
  3. trouble using fractions
    By jeanOliver in forum New To Java
    Replies: 2
    Last Post: 09-06-2011, 08:48 PM
  4. Reducing fractions
    By SwEeTAcTioN in forum New To Java
    Replies: 10
    Last Post: 01-13-2010, 04:51 PM
  5. NumberFormat which leave fractions as they are
    By itaipee in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2009, 06:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •