Thread: fractions and whole numbers

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1.2/1.2 == 1/1 == 1
    Apparently it makes mathematical sense even to you.

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    OK, I'll just explain more clearly what I am asking: what the part of the program in question does is it divides two numbers. Then it is supposed to test whether the quoteint is a whole number. if it is, it displays the whole number, if it isn't, then it is supposed to convert the decimal to a simplified fraction and show me that.

  3. #18
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Quote Originally Posted by 7stud
    Apparently it makes mathematical sense even to you.
    What he was saying was that 1.2/1.2 == 6/5

  4. #19
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Lets jsut forget I said that...it is late!

  5. #20
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Ok, I'll just explain what the program does: first, it divides two numbers. The result is then supposed to be tested. if it is a whole number, then the program displays the number, if it is a fraction, it displays a simplified fraction by converting the decimal to a fraction. so how do I find out if it is a whole number, and how to I make the fraction?

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    OK, I'll just explain more clearly what I am asking
    No need. You stated your problem clearly to begin with. Someone get a little untracked in this thread.

    To test wether an int is evenly divisible by another int, you can use the modulus or remainder operator(%). If the remainder is 0, then you have a whole number:

  7. #22
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Sorry for the last post--it is a double.

  8. #23
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    how do you use the remainder operator? Sorry, I am new to this.

  9. #24
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What he was saying was that 1.2/1.2 == 6/5
    No one mentioned 6/5 until after you posted that.

  10. #25
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Q[How do I convert decimals back to fractions?]q

    Boy, that is a complicated answer.

    0.25 = ¼

    But 0.25 is also 25 100ths, so the decimal form is 25/100 which simplifies to 1/4th.
    Which c++ will simplify 1/4th back to 0.25.

    See the trouble here?


    Uhh….hmmm…good question. Not easily. Maybe someone else knows a smooth trick?

  11. #26
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    int num1 = 10;
    int num2 = 3;
    
    int remainder = 10 % 3;
    cout<<remainder<<endl;
    
    if(num1 % num2 == 0)
    	cout<<num1<<"/"<<num2<<" is a whole number."<<endl;
    else
    	cout<<num1<<"/"<<num2<<" is not a whole number."<<endl;

  12. #27
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Victor4015
    OK, I'll just explain more clearly what I am asking: what the part of the program in question does is it divides two numbers. Then it is supposed to test whether the quoteint is a whole number. if it is, it displays the whole number, if it isn't, then it is supposed to convert the decimal to a simplified fraction and show me that.
    In other words you want it to divide something like let's say, 14/4 and instead of it giving you 3.5, you want 3 1/2.

    This is fairly simple if you know how to mathmatically convert the decimal into a fraction.

    Do the calculation into a float, then do the same calculation casting the values and placing into an int. Subract the float from the int and put it into the float. You now have your remaining decimal. Multiply that times 100, and cast as an int to knock off the rest of the decimal places. Then put that over 100 and simplify from there.
    Sent from my iPad®

  13. #28
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Thank you very much, but then how would the program know to simpliy 50/100? Also, what about a fraction like 0.3333333...? I know how to do this mathematically, but how do I do it in the program?

  14. #29
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I know how to do this mathematically
    How? Post a detailed list of steps.

  15. #30
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed