Find LCM of rational numbers
Given an array of rational numbers, the task is to find the LCM of these numbers.
Examples:
Input : vect[] = {2/7, 3/14, 5/3}
Output : 30/1
Input : vect[] = {3/14, 5/3}
Output : 15/1
Input : vect[] = {3/4, 3/2}
Output : 3/2
First find the lcm of all numerator of rational number then find the gcd of all the denominator of rational number then divide lcm of all numerator/ gcd of all the denominator this the lcm of rational number’s.
Formula:-
LCM of all the numerator of Rational number's
lcm = -----------------------------------------------
GCD of all the denominator of Rational number's
// CPP program to find LCM of given array #include <bits/stdc++.h> using namespace std; // get lcm of two numbers int LCM(int a, int b) { return (a * b) / (__gcd(a, b)); } // Finds LCM of numerators int lcmOfNumerator(vector<pair<int, int> > vect) { // calculate the lcm of all numerators int lcm = vect[0].first; for (int i = 1; i < vect.size(); i++) lcm = LCM(vect[i].first, lcm); // return all numerator lcm return lcm; } // Get GCD of all the denominators int gcdOfDemoninators(vector<pair<int, int> > vect) { // calculate the gcd of all the denominators int gcd = vect[0].second; for (int i = 1; i < vect.size(); i++) gcd = __gcd(vect[i].second, gcd); // return all denominator gcd return gcd; } // find lcm of all the rational number void lcmOfRationals(vector<pair<int, int> > vect) { // return the LCM of all numerator/ GCD of all // denominator cout << lcmOfNumerator(vect) << "/" << gcdOfDemoninators(vect); } // Driver code int main() { vector<pair<int, int> > vect; // give rational number 2/7, 3/14, 5/3 // make pair as a numerator and denominator 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; } |
chevron_right
filter_none
Output:
30/1
Recommended Posts:
- Find max of two Rational numbers
- HCF of array of fractions (or rational numbers)
- N-th term of George Cantor set of rational numbers
- Algorithm to generate positive rational numbers
- Binary Search for Rational Numbers without using floating point arithmetic
- Maximum rational number (or fraction) from an array
- as_integer_ratio() in Python for reduced fraction of a given rational
- Given pairwise sum of n numbers, find the numbers
- Given two numbers a and b find all x such that a % x = b
- Find two numbers whose sum and GCD are given
- Program to find LCM of 2 numbers without using GCD
- Find the sum of first N odd Fibonacci numbers
- Find the XOR of first N Prime Numbers
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find XOR of numbers from the range [L, R]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



