Making a number to decimal form

  • Thread starter Thread starter soul5
  • Start date Start date
  • Tags Tags
    Form
AI Thread Summary
The discussion revolves around simulating the LaTeX \frac{}{} command in a C program to display fractions in both fractional and decimal forms. The original poster seeks guidance on how to prompt users for input and format the output correctly. They initially attempt to use a complex scanf statement but receive feedback suggesting a simpler approach. The conversation clarifies that the goal is to read two integers representing a fraction, display it in a specific format, and convert it to a decimal. Participants discuss the need for the program to maintain the fraction as integers while also providing a decimal equivalent, highlighting the importance of clarity in the output format. The overall aim is to replicate the functionality of displaying fractions similar to how calculators do, while also converting them to float or double for decimal representation.
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?
 
Technology 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
 
1.25 is just float to me.
 
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...
 
I think it is just about simulating LaTeX \frac output in ASCII.
 
Back
Top