fractions and whole numbers

This is a discussion on fractions and whole numbers within the C++ Programming forums, part of the General Programming Boards category; How do I get a program to simplify a fraction, and how do I get a program to recognize if ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    Question fractions and whole numbers

    How do I get a program to simplify a fraction, and how do I get a program to recognize if a fraction is a whole number? There is probably a simple solution, but I am oblivious. Thanks alot.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    For the whole number thing you could just divide the two numbers and if that is+ 1 then...

    Code:
    no.3 == no.1 / no.2;
    if (no.3 == 1) {
    //stuff
    }
    else {
    //stuff
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    sorry, but I meant if a number is a whole number

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    yes if you divide the two numbers in the fraction and they equal one then it is a whole number.
    20/20 is a whole number 20/20 = 1

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Ah right I might have got you:

    if no.1 > no.2

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    yeah, and 1.2/1.2=1, and 1.2 isn't a whole number

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    fractions cannot be 1.2 end of

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    you cannot have 1.2/1.2 it does nto make mathematical sense!

  9. #9
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    C++ converts fractions to decimal representation and automatically simplifies to lowest term.

    Code:
    cout << 3.0/9.0;
    Results in .3333333 what is 1/3, not 3/9 (which 1/3 is just the simplified form).

    It also automatically recognizes if it is a whole number.

    Code:
    cout << 8.0/4.0;
    Gives you 2, not 2.0000000

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    No, you don't understand. I want the program to test if a number is a whole number. it will only accept integers. so if it isn't supposed to output numbers like 1.2, but it can output numbers like 56.

  11. #11
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    you cannot have 1.2/1.2 it does nto make mathematical sense!
    It makes perfect mathmatical sense.
    1.2 is just 1 and 1/5, or 6/5

    6/5 over 6/5 is perfectly legal. Or (6/5) / 9 is perfectly legal.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    How do I convert decimals back to fractions?

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    yes but you cannot have 1.2/1.2

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    1.2/1.2 == 1/1 == 1

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    you cannot have 1.2/1.2 it does nto make mathematical sense!
    Why not?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21