Find LCM of rational numbers | CPP program to find LCM of given array ; get lcm of two numbers ; Finds LCM of numerators ; calculate the lcm of all numerators ; return all numerator lcm ; Get GCD of all the denominators ; calculate the gcd of all the denominators ; return all denominator gcd ; find lcm of all the rational number ; return the LCM of all numerator / GCD of all denominator ; Driver code ; give rational number 2 / 7 , 3 / 14 , 5 / 3 make pair as a numerator and denominator
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LCM ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } int lcmOfNumerator ( vector < pair < int , int > > vect ) { int lcm = vect [ 0 ] . first ; for ( int i = 1 ; i < vect . size ( ) ; i ++ ) lcm = LCM ( vect [ i ] . first , lcm ) ; return lcm ; } int gcdOfDemoninators ( vector < pair < int , int > > vect ) { int gcd = vect [ 0 ] . second ; for ( int i = 1 ; i < vect . size ( ) ; i ++ ) gcd = __gcd ( vect [ i ] . second , gcd ) ; return gcd ; } void lcmOfRationals ( vector < pair < int , int > > vect ) { cout << lcmOfNumerator ( vect ) << " / " << gcdOfDemoninators ( vect ) ; } int main ( ) { vector < pair < int , int > > vect ; vect . push_back ( make_pair ( 2 , 7 ) ) ; vect . push_back ( make_pair ( 3 , 14 ) ) ; vect . push_back ( make_pair ( 5 , 3 ) ) ; lcmOfRationals ( vect ) ; return 0 ; }