- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

# C++ Program for GCD of more than two (or array) numbers?

The common divisor of two numbers are the numbers that are divisors of both of them.

For example, the divisors of 12 are 1, 2, 3, 4, 6, 12. The divisors of 18 are 1, 2, 3, 6, 9, 18. Thus, the common divisors of 12 and 18 are 1, 2, 3, 6. The greatest among these is, perhaps unsurprisingly, called the of 12 and 18. The usual mathematical notation for the greatest common divisor of two integers a and b are denoted by (a, b). Hence, (12, 18) = 6.

The greatest common divisor is important for many reasons. For example, it can be used to calculate the of two numbers, i.e., the smallest positive integer that is a multiple of these numbers. The least common multiple of the numbers a and b can be calculated as a*b*(a, b)

For example, the least common multiple of 12 and 18 is 12*18*(12, 18)=12*18*6

Input: 4, 10, 16, 14 Output: 2

## Explanation

GCD of two or more integers is the largest integer that can exactly divide both numbers (without a remainder).

## Example

#include <iostream> using namespace std; int gcd(int a,int b) { int temp; while(b > 0) { temp = b; b = a % b; a = temp; } return a; } int main() { int a[] = {4, 10, 16, 14}; int n = 4; int r = a[0]; for(int i=1; i<n; i++) { r = gcd(r, a[i]); } cout << r << endl; return 0; }

## Output

4

- Related Questions & Answers
- C++ Program for GCD 0.of more than two (or array) numbers?
- Python Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
- GCD of more than two (or array) numbers in Python Program
- Finding LCM of more than two (or array) numbers without using GCD in C++
- Program to find GCD or HCF of two numbers in C++
- Java program to find the GCD or HCF of two numbers
- Find GCD of two numbers
- Java Program to Find GCD of two Numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure in C++
- C# program to concat two or more Lists
- C# program to find Union of two or more Dictionaries
- C# program to find Union of two or more Lists
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- Program to compute gcd of two numbers recursively in Python