Write a program that prompts the user to enter the numerator and denominator of
ID: 669347 • Letter: W
Question
Write a program that prompts the user to enter the numerator and denominator of a fraction number and determines whether it is a proper fraction and improper fraction. For an improper fraction number, display its mixed fraction in the form of a + b / c if b % c is not zero; otherwise, display only the integer.
Here are sample runs of the program:
Sample 1:
Enter a numerator: 16
Enter a denominator: 3
16 / 3 is an improper fraction and its mixed fraction is 5 + 1 / 3.
Sample 2:
Enter a numerator: 6
Enter a denominator: 7
6 / 7 is a proper fraction
Sample 3:
Enter a numerator: 6
Enter a denominator: 2
6 / 2 is an improper fraction and it can be reduced to 3
Explanation / Answer
Which language you want me to implement in...???
I'm doing it in C++:
#include <iostream>
using namespace std;
class FractionConvert
{
int numerator;
int denominator;
public:
FractionConvert enterFractionValue() //Reads the Fraction.
{
FractionConvert fc;
cout<<"Enter the numerator: ";
cin>>fc.numerator;
fc.denominator = 0;
while(fc.denominator == 0)
{
cout<<"Enter the denominator: ";
cin>>fc.denominator;
}
return fc;
}
int isProper(FractionConvert f)
{
if(f.numerator < f.denominator)
return 1;
return 0;
}
void displayFraction(FractionConvert f) //Displays the Fraction in the prescribed format.
{
int quotient, remainder;
if(isProper(f))
cout<<f.numerator<<"/"<<f.denominator<<" is a proper fraction."<<endl;
else
{
cout<<f.numerator<<"/"<<f.denominator<<" is an improper fraction and ";
if(f.numerator % f.denominator == 0)
cout<<"it can be reduced to "<<f.numerator/f.denominator<<endl;
else
{
quotient = f.numerator / f.denominator;
remainder = f.numerator % f.denominator;
for(int i=2;i<=remainder;i++)
{
if(remainder % i == 0 && f.denominator % i == 0)
{
remainder /= i;
f.denominator /= i;
}
}
cout<<"its mixed fraction is "<<quotient<<" + "<<remainder<<" / "<<f.denominator<<endl;
}
}
}
};
int main()
{
FractionConvert fc;
fc = fc.enterFractionValue();
fc.displayFraction(fc);
}