Code: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.
Printable View
Code: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.
Code:What is a \frac{} command?
It's latex-speak.
You don't, at a console. If you're trying for "nice" output like on paper, just write a LaTeX file. If you need to fake it on a screen, print out "2 3/8" or something.
then do 1/4 in your program.Code:#include <iostream>
int main(){
float frac = 1/4;
std::cout<< frac;
return 0;
}
then explain what you are trying to do?
do you mean you want to parse it?
Code:int denominator, numerator;
scanf("\\frac{%d}{%d}", &numerator, &denominator);
cout << numerator << " / " << denominator;
the output?
the scanf line reads the values into denominator and numerator.
Here is what the excerise says
Code: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
and what is your question?
no one will do your homework for you.
Code:what do they mean by simulate a \frac{}{}
command?
Integer division gives you the whole number:
int result, five=5, four=4;
result = five / four;
Modulo operator gives you the remainder:
int rem = five % four;
Floating point division gives you the fraction:
float fraction = (float) rem / (float) four;
Code:
#include <iostream>
using namespace std;
int main()
{
double num1;
double num2;
int n1;
int n2;
char slash;
int spaces = 9 + n1 + n2;
cout << " enter the fraction asgiven num1/num2 ";
cin >> num1 >> slash >> num2 ;
if ( (num1 >=0) && ( num1 <= 9) )
n1 = 1;
else if ( num1 <=99 )
n1 = 2
else n
1 = 3;
if ( ( num2 >=0 ) && ( num2 <=9 ))
n2 = 1;
else if ( ( n2 <= 99 )
n2= 2;
else
n2= 3
if ( ( n1 > 0 ) && ( n1 > 999 ) || ( n2 > 0 ) &&
What do I do next?
fix your indentations next, and choose sensible names for you variables, and post the whole code
Now?Code:
#include <iostream>
using namespace std;
int main()
{
double num1;
double num2;
int n1;
int n2;
char slash;
int spaces = 9 + n1 + n2;
cout << " enter the fraction asgiven num1/num2 ";
cin >> num1 >> slash >> num2 ;
if ( (num1 >=0) && ( num1 <= 9) )
n1 = 1;
else if ( num1 <=99 )
n1 = 2
else
n1 = 3;
if ( ( num2 >=0 ) && ( num2 <=9 ))
n2 = 1;
else if ( ( n2 <= 99 )
n2= 2;
else
n2= 3
if ( ( n1 > 0 ) && ( n1 > 999 ) || ( n2 > 0 ) &&
indentation isn't any better, variable names aren't changed, and you are still only posting half the code.