Answer to Question #345300 in C++ for Umaima

Question #345300

Create a structure called fraction. It has two members, both type int, called numerator and



denominator. Write a program that input values in two variables of type fraction. Add both



fractions and print the result in fraction format.

1
Expert's answer
2022-05-26T14:07:49-0400
#include <iostream>
using namespace std;


struct fraction{
    int numerator;
    int denominator;
    


    fraction operator+(fraction b){
        return fraction{numerator+b.numerator, denominator+b.denominator};
    }
    
};
ostream& operator<<(ostream& os, const fraction& fr)
{
    os << "["<<fr.numerator << ',' << fr.denominator << ']';
    return os;
}
int main()
{
    fraction f1, f2, result;
    
    cout<<"Enter numerator1: ";
    cin>>f1.numerator;
    cout<<"Enter denominator1: ";
    cin>>f1.denominator;


    cout<<"Enter numerator2: ";
    cin>>f2.numerator;
    cout<<"Enter denominator2: ";
    cin>>f2.denominator;
        
    result = f1+f2;
    cout<< result;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS