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)

`// 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

Please refer complete article on GCD of more than two (or array) numbers for more details!

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.

## Recommended Posts:

- Java Program for GCD of more than two (or array) numbers
- GCD of more than two (or array) numbers
- Finding LCM of more than two (or array) numbers without using GCD
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Smallest subsequence having GCD equal to GCD of given array
- Number of triangles in a plane if no more than two points are collinear
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Numbers having difference with digit sum more than s
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Remove minimum elements from the array such that 2*min becomes more than max
- Find all array elements occurring more than ⌊N/3⌋ times
- Program to find GCD or HCF of two numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure
- GCD of array is greater than one
- Count of divisors having more set bits than quotient on dividing N
- Prefixes with more a than b
- Check if a cell can be visited more than once in a String
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Check if the frequency of any character is more than half the length of the string