Making a number to decimal form

  • Thread starter soul5
  • Start date
  • Tags
    Form
In summary, the conversation discusses an exercise to simulate a \frac{}{} command, which is used to display fractions in both fraction and decimal form. The program reads two integers and prompts the user to enter a fraction in the form n1/n2. The output should display the fraction in both forms. The discussion also mentions the possibility of converting the fraction back to a float or double.
  • #1
soul5
64
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
  • #2
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.
 
  • #3
soul5 said:
how do I make it to decimal?

When you write decimal, do you mean float?
 
  • #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
 
  • #5
1.25 is just float to me.
 
  • #6
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.
 
  • #7
I still don't understand the OP's question...
 
  • #8
I think it is just about simulating LaTeX \frac output in ASCII.
 

1. How do you convert a whole number to decimal form?

To convert a whole number to decimal form, simply place a decimal point after the number and add a zero to the right of the decimal point. For example, if the whole number is 5, the decimal form would be 5.0.

2. What is the purpose of converting a number to decimal form?

Converting a number to decimal form allows for a more precise representation of the number. It also allows for easier comparison and calculation with other numbers in decimal form.

3. Can any number be converted to decimal form?

Yes, any number can be converted to decimal form. This includes whole numbers, fractions, and even irrational numbers like pi.

4. How do you convert a fraction to decimal form?

To convert a fraction to decimal form, divide the numerator by the denominator and express the result as a decimal. If the fraction is a repeating decimal, you can use long division or a calculator to get a more accurate decimal form.

5. Can converting a number to decimal form change the value of the number?

No, converting a number to decimal form does not change the value of the number. It only changes the way it is represented. For example, the whole number 5 and the decimal form 5.0 have the same value of 5.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
1
Views
950
  • Programming and Computer Science
Replies
2
Views
873
  • Programming and Computer Science
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
954
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top