
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find GCD and LCM of N Numbers in C++
The gcd refers to 'Greatest Common Divisor', i.e. greatest common number which can divide all the given numbers. The lcm refers to the 'Least Common Multiple' i.e. the lowest common multiple of all the numbers. To find the gcd and lcm of n numbers in C++, we can use various approaches like iterative approach, or built-in C++ functions.
In this article, we are having 'n' number of elements, our task is to find the gcd and lcm of n number of elements using C++.
Example
Here is an example of GCD and LCM of 4 numbers:
Input: Numbers = {2, 3, 4, 7, 5} GCD(2, 3) = 1, GCD(1, 4) = 1, GCD(1, 7) = 1, GCD(1, 5) = 1 GCD of given numbers: 1 LCM(2, 3) = (2*3)/GCD(2, 3) = 6 LCM(6, 4) = (6*4)/GCD(6, 4) = 12 LCM(12, 7) = (12*7)/GCD(12, 7) = 84 LCM(84, 5) = (84*5)/GCD(84, 5) = 420 LCM of given numbers: 420 Output: GCD of given numbers: 1 LCM of given numbers: 420
Here is a list of approaches to find the gcd and lcm of n numbers in C++ which we will be discussing in this article with stepwise explanation and complete example codes.
Using Iteration
In this approach, we have used an iterative approach to find the gcd and lcm of n numbers.
- The gcd function calculates the gcd of two numbers. It runs till 'b' becomes 0 and keeps updating 'a' with 'b' using 't' and updates 'b' with the remainder of 'a' and 'b'.
- The lcm function calculates the lcm of two numbers. We have divided the gcd from the product of both numbers to get the lcm.
- Then in the main() function, we iterative calculate and store the gcd and lcm of all the elements of the array in gcdN and lcmN.
Example
Here is an example of implementing the above mentioned-steps to find the gcd and lcm of n numbers using an iterative approach.
#include <iostream> using namespace std; int gcd(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a; } int lcm(int a, int b) { return (a / gcd(a, b)) * b; } int main() { int numbers[] = {2, 3, 4, 7, 5}; int n = sizeof(numbers) / sizeof(numbers[0]); int gcdN = numbers[0], lcmN = numbers[0]; for (int i = 1; i < n; i++) { gcdN = gcd(gcdN, numbers[i]); lcmN = lcm(lcmN, numbers[i]); } cout << "GCD of given numbers: " << gcdN << "\nLCM of given numbers: " << lcmN << endl; }
Using Recursion
To find the gcd and lcm of n numbers, we have used the recursive approach.
- The gcd() function accepts two numbers('a' and 'b') as arguments and returns 'b' and the remainder of 'a' and 'b' using "a % b". The function recursively calls itself until 'b' becomes 0.
- The lcm function calculates the lcm of two numbers. We have divided the gcd from the product of both numbers to get the lcm.
- The gcdRec() function first checks the index if it has reached the last element. If not the last element, then recursively calls the gcd() function to calculate the gcd of the current element and the result of the remaining elements.
- We repeat the above step to calculate the lcm in the lcmRec() function.
- Then in the main() function, we call the gcdRec() and lcmRec() functions to get the gcd and lcm of all the elements of the array.
Example
The following example implements the above steps to find the gcd and lcm of n numbers using a recursive approach.
#include <iostream> using namespace std; int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } int lcm(int a, int b) { return (a / gcd(a, b)) * b; } int gcdRec(int arr[], int n, int index = 0) { if (index == n - 1) { return arr[index]; } return gcd(arr[index], gcdRec(arr, n, index + 1)); } int lcmRec(int arr[], int n, int index = 0, int current = 1) { if (index == n) { return current; } return lcmRec(arr, n, index + 1, lcm(current, arr[index])); } int main() { int arr[] = {2, 3, 4, 7, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << "GCD of given numbers: " << gcdRec(arr, n) << "\nLCM of given numbers: " << lcmRec(arr, n) << endl; return 0; }
Using gcd() and lcm() Functions
The third approach uses the built-in functions of <numeric> header i.e. gcd() and lcm() functions to find the gcd and lcm of n numbers.
Example
Below is an example of using built-in gcd() and lcm() functions to find the gcd and lcm of n numbers.
#include <iostream> #include <numeric> using namespace std; int main() { int numbers[] = {2, 3, 4, 7, 5}; int n = sizeof(numbers) / sizeof(numbers[0]); int gcdN = numbers[0], lcmN = numbers[0]; for (int i = 1; i < n; i++) { gcdN = gcd(gcdN, numbers[i]); lcmN = lcm(lcmN, numbers[i]); } cout << "GCD of given numbers: " << gcdN << "\nLCM of given numbers: " << lcmN << endl; }
Using __gcd() Function
This approach uses the __gcd() function of <algorithm> header to find the gcd and using this gcd value we have calculated the lcm.
Example
The following example finds the gcd and lcm of n numbers using the __gcd function.
#include <iostream> #include <algorithm> using namespace std; int gcdN(int arr[], int n) { int result = arr[0]; for(int i = 1; i < n; ++i) result = __gcd(result, arr[i]); return result; } int lcmN(int arr[], int n) { int result = arr[0]; for(int i = 1; i < n; ++i) result = (result * arr[i]) / __gcd(result, arr[i]); return result; } int main() { int arr[] = {2, 3, 4, 7, 5}; int n = sizeof(arr)/sizeof(arr[0]); cout << "GCD of given numbers: " << gcdN(arr, n) << endl; cout << "LCM of given numbers: " << lcmN(arr, n) << endl; return 0; }