Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Making a number to decimal form

  1. Sep 29, 2008 #1
    I'm doing an excerise to make factions, but I need to simulate a \frac{}{}
    command. To display numbers in faction AND decimal form. How do I simulate a
    \frac{}{} command? Thanks. Like I have to prompt the user.

    Code (Text):
    int denominator, numerator;
    scanf("\\frac{%d}{%d}", &numerator, &denominator);
    cout << numerator << " / " << denominator;

    Enter fraction in the form n1/n2:
    1
    \frac{1}{2} is --
    2

    or 0.5

    how do I make it to decimal?
     
  2. jcsd
  3. Sep 29, 2008 #2

    zyh

    User Avatar

    Hi, I think the "scanf" statement is too complex, I just rewrite your code:
    Code (Text):
    int main(int argc, char* argv[])
    {
        int denominator, numerator;
        scanf("%d,%d", &numerator, &denominator);
        printf("%d / %d \n",numerator,denominator);

    }
    What does "simulate a \frac{}{} command " means? C treat any number is decimal base by default.
     
  4. Sep 29, 2008 #3

    Borek

    User Avatar

    Staff: Mentor

    When you write decimal, do you mean float?
     
  5. Sep 29, 2008 #4
    Here is what the excerise says

    One such command displays fractions. For example 5/4 is entered
    "\frac{5}{4}". Write a computer program that simulates a \frac{}{}
    command. The program reads two integers n1 and n2


    EXAMPLE 1:
    Enter fraction in the form n1/n2: 5/4
    .................... 5
    \frac{5}{4} is --
    ................... 4

    .... . 1
    or 1 --
    .... . 4

    or 1.25
     
  6. Sep 30, 2008 #5

    Borek

    User Avatar

    Staff: Mentor

    1.25 is just float to me.
     
  7. Sep 30, 2008 #6

    rcgldr

    User Avatar
    Homework Helper

    Some calculators have a fraction feature that will maintain numbers as integer fractions (within reason). My guess is that the goal here is create the equivalent of this with a C program, then have another feature to convert the fraction back to a float or double.
     
  8. Sep 30, 2008 #7

    zyh

    User Avatar

    I still don't understand the OP's question...
     
  9. Sep 30, 2008 #8

    Borek

    User Avatar

    Staff: Mentor

    I think it is just about simulating LaTeX \frac output in ASCII.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?