C++ Program for GCD of more than two (or array) numbers Last Updated : 20 Mar, 2023 Comments Improve Suggest changes Like Article Like Report The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) CPP // C++ program to find GCD of two or // more numbers #include <bits/stdc++.h> using namespace std; // Function to return gcd of a and b int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // Function to find gcd of array of // numbers int findGCD(int arr[], int n) { int result = arr[0]; for (int i = 1; i < n; i++) result = gcd(arr[i], result); return result; } // Driven code int main() { int arr[] = { 2, 4, 6, 8, 16 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findGCD(arr, n) << endl; return 0; } C# using System; public class Program { // Function to return gcd of a and b public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // Function to find gcd of array of numbers public static int findGCD(int[] arr, int n) { int result = arr[0]; for (int i = 1; i < n; i++) result = gcd(arr[i], result); return result; } // Driven code public static void Main() { int[] arr = { 2, 4, 6, 8, 16 }; int n = arr.Length; Console.WriteLine(findGCD(arr, n)); } } //this code is added by snehalsalokhe Output:2 Time Complexity: O(N * log(N)), where N is the largest element of the arrayAuxiliary Space: O(1), ignoring the stack space used in recursion Implementation of the same code with Iterative GCD Function C++ // C++ program to find GCD of two or // more numbers #include <bits/stdc++.h> using namespace std; // Function to return gcd of a and b int gcd(int a, int b) { int result = min(a, b); // Find Minimum of a nd b while (result > 0) { if (a % result == 0 && b % result == 0) { break; } result--; } return result; // return gcd of a nd b } // Function to find gcd of array of // numbers int findGCD(int arr[], int n) { int result = arr[0]; for (int i = 1; i < n; i++) result = gcd(arr[i], result); return result; } // Driven code int main() { int arr[] = { 2, 4, 6, 8, 16 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findGCD(arr, n) << endl; return 0; } Output2 Time Complexity: O(N * log(N)), where N is the largest element of the arrayAuxiliary Space: O(1) Please refer complete article on GCD of more than two (or array) numbers for more details! Comment More infoAdvertise with us Next Article C++ Program for GCD of more than two (or array) numbers K kartik Follow Improve Article Tags : Mathematical C++ Programs DSA GCD-LCM Practice Tags : Mathematical Similar Reads C++ Program for Common Divisors of Two Numbers Given two integer numbers, the task is to find the count of all common divisors of given numbers. Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b = 17 Output: 1 // all common divisors are 1 Input : a = 20, b = 36 Output: 3 // all common divisors a 2 min read C++ Program to Find the GCDs of given index ranges in an array Write a C++ program for a given array a[0 . . . n-1], the task is to find the GCD from index qs (query start) to qe (query end) where 0 <= qs <= qe <= n-1. Example: Input: arr[] = {2, 3, 60, 90, 50};Index Ranges: {1, 3}, {2, 4}, {0, 2}Output: GCDs of given ranges are 3, 10, 1Explanation: El 5 min read Find the ratio of LCM to GCD of a given Array Given an array arr[] of positive integers, the task is to find the ratio of LCM and GCD of the given array.Examples: Input: arr[] = {2, 3, 5, 9} Output: 90:1 Explanation: The GCD of the given array is 1 and the LCM is 90. Therefore, the ratio is evaluated as 90:1.Input: arr[] = {6, 12, 36} Output: 6 7 min read GCD of Two Numbers in C++ GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that exactly divides both numbers. In this article, we will learn to write a C++ program to find the GCD of two numbers.Example:Input: a = 12, b = 16Output: 4Explanation: As 4 is the largest number whic 3 min read Count common prime factors of two numbers Given two integer A and B , the task is to find the count of common factors of two numbers where factors are prime.Examples: Input: A = 6, B = 12 Output: 2 2 and 3 are the only common prime divisors of 6 and 12Input: A = 4, B = 8 Output: 1 Naive Approach: Iterate from 1 to min(A, B) and check whethe 10 min read Like