
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find LCM of rational number in C++
Here we will see how to find the LCM of Rational numbers. We have a list of rational numbers. Suppose the list is like {2/7, 3/14, 5/3}, then the LCM will be 30/1.
To solve this problem, we have to calculate LCM of all numerators, then gcd of all denominators, then the LCM of rational numbers, will be like −
$$LCM =\frac{LCM\:of\:all\:𝑛𝑢𝑚𝑒𝑟𝑎𝑡𝑜𝑟𝑠}{GCD\:of\:all\:𝑑𝑒𝑛𝑜𝑚𝑖𝑛𝑎𝑡𝑜𝑟𝑠}$$
Example
#include <iostream> #include <vector> #include <algorithm> using namespace std; int LCM(int a, int b) { return (a * b) / (__gcd(a, b)); } int numeratorLCM(vector<pair<int, int> > vect) { int result = vect[0].first; for (int i = 1; i < vect.size(); i++) result = LCM(vect[i].first, result); return result; } int denominatorGCD(vector<pair<int, int> >vect) { int res = vect[0].second; for (int i = 1; i < vect.size(); i++) res = __gcd(vect[i].second, res); return res; } void rationalLCM(vector<pair<int, int> > vect) { cout << numeratorLCM(vect) << "/"<< denominatorGCD(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)); cout << "LCM of rational numbers: "; rationalLCM(vect); }
Output
LCM of rational numbers: 30/1
- Related Articles
- What are rational numbers? Types of rational number.
- Find the other number when LCM and HCF given in C++
- Find LCM of two numbers
- Find five rational number between 1 and 2
- Find a rational number between 2 and 4.
- Find a rational number between $-3$ and $1$.
- Find any five rational number less than 1.
- Multiplicative Inverse In Rational Number
- Find LCM of 29 and 93.
- Find LCM of 12 and 15.
- Find the cube root of the following rational number.$frac{4913}{3375}$.
- What is LCM and how do we find the LCM of given numbers?
- Find a rational number between $sqrt{2}$ and $sqrt{3}$.
- Find two rational number between -4/3 and 3/7
- Find LCM of 524, 128 and 56.

Advertisements