How to reduce fractions

So far im getting my fraction to equal a number.

Code:
if ((aFraction.numerator/aFraction.denominator) > 1)
calculation9= aFraction.numerator/aFraction.denominator;
cout<<"Your fraction reduced: "<<calculation9<<endl;

and say I put in 4/2 as my fraction I get 2 for my answer.
If I put in 5/2 I get 2.5, but what I want to get is 2 1/2 for my answer.

So, im just asking how I can do that. Im just having a hard time thinking of a way.
The modulo operator (%) gives you the remainder of any division.

So, 5 % 2 would equal 1, 5 % 3 would equal 2, etc.

User integer division to get the whole number.

Then use the modulo to get the remainder.

Then format the values to display it the way you want.
I'd suggest using
1
2
3
int a = Fraction.numerator%Fraction.denominator;
int b=Fraction.numerator/Fraction.denominator;
cout << b << " " << a << "//" << Fraction.denominator


The % will return the remainder of the division of numerator/denominator.
Making b an int means that 5/2 = 2, since C will simply cut out the decimals.
I got it now! Thank you very much :)

this is what i ended up with

if ((aFraction.numerator/aFraction.denominator) > 1)
calculation9= aFraction.numerator%aFraction.denominator;
calculation10= aFraction.numerator/aFraction.denominator;
cout<<"Your fraction reduced: "<<calculation10<<" "<<calculation9<<"/"<<aFraction.denominator<<endl;


and made calculation10 as int so it can only display the 1st number
Topic archived. No new replies allowed.