Open In App

GCD of Two Numbers in C++

Last Updated : 01 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

GCD or HCF of Two Numbers

1. GCD Using Simple Method

The idea is to find the minimum of the two numbers and then find the common divisor starting from the minimum number and decrementing it by 1 until a common divisor is found.

Algorithm

  • Initialize a variable result with the minimum of a and b.
  • Run a loop till the result > 0.
  • If both numbers are divisible by the variable result, break the loop.
  • Else, decrement the result variable by 1.
  • After the loop, the result variable holds the GCD of the two numbers.
  • Return the result variable.

C++ Program To Find GCD of Two Numbers Using Simple Method

C++




// C++ program to find GCD of two numbers
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return gcd of a and b
int gcd(int a, int b)
{
    // Find Minimum of a and b
    int result = min(a, b);
    while (result > 0) {
        if (a % result == 0 && b % result == 0) {
            break;
        }
        result--;
    }
  
    // Return gcd of a and b
    return result;
}
  
// Driver code
int main()
{
    int a = 98, b = 56;
    cout << "GCD of " << a << " and " << b << " is "
         << gcd(a, b);
    return 0;
}
// This code is contributed by Suruchi Kumari


Output

GCD of 98 and 56 is 14

2. GCD Using Euclidean Algorithm

The Euclidean algorithm is an efficient method to find the GCD of two numbers. It works on the principle that the GCD of two numbers remains the same if the greater number is replaced by the difference between the two numbers.

Algorithm

  • Define a function that takes two integers to find their GCD.
  • If a is equal to 0, then the GCD of a and b is b.
  • If b is equal to 0, then the GCD of a and b is a.
  • If a and b are equal, the GCD is a or b.
  • If a > b, call the gcd function recursively with parameters (a – b, b).
  • Else, call the gcd function recursively with parameters (a, b-a).

C++ Program to Find GCD of Two Numbers Using Euclidean Algorithm

C++




// C++ program to find GCD
// of two numbers
#include <iostream>
using namespace std;
  
// Recursive function to return
// gcd of a and b
int gcd(int a, int b)
{
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
  
    // base case
    if (a == b)
        return a;
  
    // a is greater
    if (a > b)
        return gcd(a - b, b);
    return gcd(a, b - a);
}
  
// Driver code
int main()
{
    int a = 98, b = 56;
    cout << "GCD of " << a << " and " << b << " is "
         << gcd(a, b);
    return 0;
}


Output

GCD of 98 and 56 is 14

Explanation

a = 98 and b = 56
a > b so put a = a - b and b remains the same.
So a = 98 - 56 = 42 and b = 56. 
Now b > a so b = b - a and a is same b= 56-42 = 14 & a= 42. 
42 is 3 times 14
HCF is 14.

Complexity Analysis

  • Time Complexity: O(min(a,b))
  • Auxiliary Space: O(min(a,b))

3. GCD Using Inbuilt Function in C++

In C++ Standard Library, there is an inbuilt function __gcd() in <algorithm> header file to find the gcd of two numbers.

Syntax

 __gcd(num1, num2)

where num1 and num2 are the two numbers.

C++ Program to Find the GCD of Two Numbers Using Inbuilt Functions

C++




// CPP program to find gcd of two numbers using inbuilt
// __gcd() function
  
// #include<numeric> for C++17
#include <algorithm>
#include <iostream>
  
using namespace std;
  
int main()
{
    cout << "gcd(10, 20) = " << __gcd(10, 20) << endl;
}


Output

gcd(10, 20) = 10

Complexity Analysis

  • Time Complexity: O(log n)
  • Auxiliary Space: O(1)

Please refer to the complete article Program to Find GCD or HCF of Two Numbers for more methods to find the GCD of two numbers.

Related Articles



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads