Making a number to decimal form

  • Thread starter Thread starter soul5
  • Start date Start date
  • Tags Tags
    Form
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 8K views
soul5
Messages
63
Reaction score
0
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:
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?
 
Physics news on Phys.org
Hi, I think the "scanf" statement is too complex, I just rewrite your code:
Code:
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.
 
soul5 said:
how do I make it to decimal?

When you write decimal, do you mean float?
 
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
 
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.
 
I still don't understand the OP's question...