- Use integer value of numerator and the denominator.
- Provide constructor to enable class and this should define value
- Additonal of two Rational number
- Subtraction of Two Rational number
- Multiplication of two Rational number
- Division of Two Rational number
- print as a/b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this code do arthmatic operation in rational numbers | |
// All right recived by simulation-hub | |
#include<iostream> | |
using namespace std; | |
class Rational | |
{ | |
private: | |
int numertor; | |
int denominator; | |
public: | |
Rational(){} // default constructor | |
Rational(int a,int b){ | |
if(b>0){ | |
numertor=a;denominator=b; | |
} | |
else | |
{ | |
cout<<"Infinite value of rational number .. denominator should not be zero"<<endl; | |
cout<<"All value will be garbag values"<<endl; | |
} | |
} // set value througth constuctor | |
void add(Rational r1,Rational r2)// function for addtion | |
{ | |
this->denominator=r1.denominator*r2.denominator; | |
this->numertor=r1.numertor*(this->denominator/r1.denominator)+r2.numertor*(this->denominator/r2.denominator); | |
cout<<"Rational Number addtion:"<<this->numertor<<"/"<<this->denominator<<endl; | |
} | |
void sub(Rational r1,Rational r2)// function for subtraction | |
{ | |
this->denominator=r1.denominator*r2.denominator; | |
this->numertor=r1.numertor*(this->denominator/r1.denominator)-r2.numertor*(this->denominator/r2.denominator); | |
cout<<"Rational Number Subtraction:"<<this->numertor<<"/"<<this->denominator<<endl; | |
} | |
void Multi(Rational r1,Rational r2)// for multiplication | |
{ | |
this->denominator=r1.denominator*r2.denominator; | |
this->numertor=r1.numertor*r2.numertor; | |
cout<<"Rational Number after multiplication :"<<this->numertor<<"/"<<this->denominator<<endl; | |
} | |
void Div(Rational r1,Rational r2)// division | |
{ | |
this->denominator=r1.denominator*r2.numertor; | |
this->numertor=r1.numertor*r2.denominator; | |
cout<<"Rational Number after division :"<<this->numertor<<"/"<<this->denominator<<endl; | |
} | |
void print() | |
{ | |
cout<<"Rational Number :"<<numertor<<"/"<<denominator<<endl; | |
} | |
}; | |
int main() | |
{ | |
Rational r1(2,3),r2(3,5),r3; | |
r1.print(); | |
r2.print(); | |
r3.add(r1,r2); | |
r3.sub(r1,r2); | |
r3.Multi(r1,r2); | |
r3.Div(r1,r2); | |
} |
Rational Number :2/3 Rational Number :3/5 Rational Number addtion:19/15 Rational Number Subtraction:1/15 Rational Number after multiplication :6/15 Rational Number after division :10/9
0 Comments
If you have any doubt and suggestion Please let me know