# Thread: Times function for rational numbers

1. ## Times function for rational numbers

hi, i have everything working in this program expect when i made my fraction simplifying function, the multiplication of the two fractions all of a sudden do not appear.

Can you help??

Code:
```#include "Rational.h"
#include "math.h"
#include <iostream>

using namespace std;

Rational::Rational(int N, int D)     //gets num and den
{   numerator = N;
denominator = D;  }

int Rational::num()
{return numerator;}

int Rational::den()
{return denominator;}

{int temp = num()*f2.den();
int temp2 = (den()*f2.num()); //num times by the den of the second and adds it to the den of the first times with the num of the second
int fnum= temp+temp2;
int fden = den()*f2.den();       //den of first times den of second
Rational rational(fnum,fden);  //gets final num and den
simplify(rational); //simplifies the final num and den
}

int Rational::subtract(Rational f2) //subtracts the two fractions
{int temp = num()*f2.den();
int temp2 =den()*f2.num(); //num times second den subtracted by the den of the first and the num of the second
int fnum= temp+temp2;
int fden = den()*f2.den();  //den of the first and den of the second
Rational rational(fnum,fden); //gets final num and den
cout<< "Subtracted: ";
simplify(rational); //simplifies the final num and den
}

int Rational::times(Rational f2) //times the two fractions
{   int fnum =num()*f2.num();  //num of the first times the num of the second
int fden =den()*f2.den();  //den of the first times den of the second
Rational rational(fnum,fden); //gets the final num and den
cout << "Timed:      ";
simplify(rational);        //simplifies the final num and den
}

int Rational::divide(Rational f2) //divides the two fractions
{   int fnum =num()*f2.den();   //num times den of second
int fden =den()*f2.num();   //den of first times num of second
Rational rational(fnum,fden); //gets final num and den
cout << "Divided:    ";
simplify(rational);           //simplifies the final num and den
}

void Rational::simplify(Rational rational)
{    int num1=rational.num();                 // takes the final num and den
int num2=rational.den();
for(int x = 1; x <= num1; ++x){ //for loop, increments until the number is greater than the numerator of the fraction
if(num1 % x == 0 && num2 % x == 0){ //checks if each number is divisible;)
while((num1 % x == 0 && num2 % x == 0) && x != 1){ //Incase it isn't in lowest terms
int fnum =(num1 /= x); //Does the dividing
int fden= (num2 /= x); //This is for the denomenator
Rational end (fnum,fden);
show (end);                   //shows the final ans
}
}
}
}

void Rational::show(Rational end) //shows the fractions
{  int fnum=end.num();
int fden=end.den();
cout <<"**"<<fnum<<"/"<<fden<<"**" <<endl;
}

bool Rational::compare(Rational f2)
//compares the two fractions to see if they are equal
{
float initial=(float)num()/den();
float final= (float)f2.num()/f2.den();
if (initial==final) return true;
else return false;
}```

2. Suppose that the fraction is already in lowest terms (cannot be simplified). Then there is no x that evenly divides both the numerator and the denominator, so the if condition
(num1 % x == 0 && num2 % x == 0) is never satisfied, that block of code is not executed and the function does not display anything.

Suppose, on the other hand, that there is such an x. Then the if condition is satisfied, your program enters the while loop, which is actually an infinite loop because within that loop, num1, num2 and x never change. In this case, the program will display the fraction resulting from dividing both the numerator and denominator by x, but will hang there because it can never exit that loop.