# C++ Program to find GCD of three numbers

## C++ Program to find GCD of three numbers

Here, in this section we will discuss GCD of three numbers in C++. GCD (Greatest Common Divisor) of two numbers is the largest number that divides both numbers.

Example : The GCD of 20, 45 and 30 will be :

Factors of 20 are 2 X 2 X 5

Factors of 45 are 3 X 3 X 5

Factors of 30 are 2 X 3 X 5

Common factor of 20, 45 and 30  : 5 , So the required GCD will be 5 ## Algorithm :

• Take the input of three numbers from the user.
• Store them in variable a, b and c.
• Find the maximum among the three.
• Run a reverse loop from maximum number to 1.
• Check for every number if all three number is divisible by that particular number or not.
• If it is so then break the loop and print that number.
`#include <iostream>using namespace std;int gcd (int a, int b, int c) {    int maxi = 0;    maxi = max(a, max(b, c));    for(int i = maxi; i>1; i--)  {      if(( a%i == 0 ) and ( b%i == 0 ) and ( c%i == 0 ))      {          return i;          break;      }  }    return 1;}int main () {  int a, b, c;    cout<<"Enter the numbers :";  cin >> a >> b >> c;        cout << "GCD of " << a << ", " << b << " and " << c << " is " << gcd (a, b, c);    return 0;}`
`Output :Enter the numbers : 20 45 30GCD of 20, 45 and 30 is 5`