Skip to content
Related Articles
Find LCM of rational numbers
• Difficulty Level : Hard
• Last Updated : 03 Feb, 2020

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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
```

## C++

 `// CPP program to find LCM of given array``#include ``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 > vect)``{``    ``// calculate the lcm of all numerators``    ``int` `lcm = vect.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 > vect)``{``    ``// calculate the gcd of all the denominators``    ``int` `gcd = vect.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 > vect)``{``    ``// return the LCM of all numerator/ GCD of all ``    ``// denominator``    ``cout << lcmOfNumerator(vect) << ``"/"``        ``<< gcdOfDemoninators(vect);``}`` ` `// Driver code``int` `main()``{``    ``vector > 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;``}`

## Java

 `// JAVA program to find LCM of given array``import` `java.util.*;`` ` `class` `GFG``{``     ` `static` `class` `pair``{ ``    ``int` `first, second; ``    ``public` `pair(``int` `first, ``int` `second) ``    ``{ ``        ``this``.first = first; ``        ``this``.second = second; ``    ``} ``}`` ` `// get lcm of two numbers``static` `int` `LCM(``int` `a, ``int` `b)``{``    ``return` `(a * b) / (__gcd(a, b));``}``static` `int` `__gcd(``int` `a, ``int` `b) ``{ ``    ``return` `b == ``0``? a:__gcd(b, a % b);     ``}`` ` `// Finds LCM of numerators``static` `int` `lcmOfNumerator(Vector vect)``{``    ``// calculate the lcm of all numerators``    ``int` `lcm = vect.get(``0``).first;``    ``for` `(``int` `i = ``1``; i < vect.size(); i++) ``        ``lcm = LCM(vect.get(i).first, lcm); `` ` `    ``// return all numerator lcm``    ``return` `lcm;``}`` ` `// Get GCD of all the denominators``static` `int` `gcdOfDemoninators(Vector vect)``{``    ``// calculate the gcd of all the denominators``    ``int` `gcd = vect.get(``0``).second;``    ``for` `(``int` `i = ``1``; i < vect.size(); i++) ``        ``gcd = __gcd(vect.get(i).second, gcd); `` ` `    ``// return all denominator gcd``    ``return` `gcd;``}`` ` `// find lcm of all the rational number``static` `void` `lcmOfRationals(Vector vect)``{``    ``// return the LCM of all numerator/ GCD of all ``    ``// denominator``    ``System.out.print(lcmOfNumerator(vect)+ ``"/"``        ``+ gcdOfDemoninators(vect));``}`` ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``Vector vect = ``new` `Vector();`` ` `    ``// give rational number 2/7, 3/14, 5/3``    ``// make pair as a numerator and denominator``    ``vect.add(``new` `pair(``2``, ``7``));``    ``vect.add(``new` `pair(``3``, ``14``));``    ``vect.add(``new` `pair(``5``, ``3``));``    ``lcmOfRationals(vect);``}``}`` ` `// This code is contributed by Rajput-Ji`

## Python

 `# Python program to find LCM of given array ``import` `math`` ` `# get lcm of two numbers ``def` `LCM(a, b):``     ` `    ``return` `(a ``*` `b) ``/``/` `(math.gcd(a, b)) `` ` `# Finds LCM of numerators ``def` `lcmOfNumerator(vect):`` ` `    ``# calculate the lcm of all numerators ``    ``lcm ``=` `vect[``0``][``0``]``    ``for` `i ``in` `range``(``1``, ``len``(vect)):``        ``lcm ``=` `LCM(vect[i][``0``], lcm) ``     ` `    ``# return all numerator lcm ``    ``return` `lcm `` ` `# Get GCD of all the denominators ``def` `gcdOfDemoninators(vect):``     ` `    ``# calculate the gcd of all the denominators ``    ``gcd ``=` `vect[``0``][``1``] ``    ``for` `i ``in` `range``(``1``, ``len``(vect)):``        ``gcd ``=` `math.gcd(vect[i][``1``], gcd) ``         ` `    ``# return all denominator gcd ``    ``return` `gcd `` ` `# find lcm of all the rational number ``def` `lcmOfRationals(vect):``     ` `    ``# return the LCM of all numerator/ GCD of all ``    ``# denominator ``    ``print``(lcmOfNumerator(vect), ``"/"``, ``            ``gcdOfDemoninators(vect), sep ``=` `"")`` ` ` ` `# Driver code `` ` `vect ``=` `[]`` ` `# give rational number 2/7, 3/14, 5/3 ``# make pair as a numerator and denominator ``vect.append((``2``, ``7``)) ``vect.append((``3``, ``14``)) ``vect.append((``5``, ``3``)) ``lcmOfRationals(vect) `` ` `# This code is contributed by SHUBHAMSINGH10`

## C#

 `// C# program to find LCM of given array``using` `System;``using` `System.Collections.Generic;`` ` `class` `GFG``{``     ` `class` `pair``{ ``    ``public` `int` `first, second; ``    ``public` `pair(``int` `first, ``int` `second) ``    ``{ ``        ``this``.first = first; ``        ``this``.second = second; ``    ``} ``}`` ` `// get lcm of two numbers``static` `int` `LCM(``int` `a, ``int` `b)``{``    ``return` `(a * b) / (__gcd(a, b));``}``static` `int` `__gcd(``int` `a, ``int` `b) ``{ ``    ``return` `b == 0? a:__gcd(b, a % b);     ``}`` ` `// Finds LCM of numerators``static` `int` `lcmOfNumerator(List vect)``{``    ``// calculate the lcm of all numerators``    ``int` `lcm = vect.first;``    ``for` `(``int` `i = 1; i < vect.Count; i++) ``        ``lcm = LCM(vect[i].first, lcm); `` ` `    ``// return all numerator lcm``    ``return` `lcm;``}`` ` `// Get GCD of all the denominators``static` `int` `gcdOfDemoninators(List vect)``{``    ``// calculate the gcd of all the denominators``    ``int` `gcd = vect.second;``    ``for` `(``int` `i = 1; i < vect.Count; i++) ``        ``gcd = __gcd(vect[i].second, gcd); `` ` `    ``// return all denominator gcd``    ``return` `gcd;``}`` ` `// find lcm of all the rational number``static` `void` `lcmOfRationals(List vect)``{``    ``// return the LCM of all numerator/ GCD of all ``    ``// denominator``    ``Console.Write(lcmOfNumerator(vect)+ ``"/"``        ``+ gcdOfDemoninators(vect));``}`` ` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``List vect = ``new` `List();`` ` `    ``// give rational number 2/7, 3/14, 5/3``    ``// make pair as a numerator and denominator``    ``vect.Add(``new` `pair(2, 7));``    ``vect.Add(``new` `pair(3, 14));``    ``vect.Add(``new` `pair(5, 3));``    ``lcmOfRationals(vect);``}``}`` ` `// This code is contributed by 29AjayKumar`

Output:

```30/1
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up