GCD of Two Numbers in C++
Last Updated :
01 Aug, 2023
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.
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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
int result = min(a, b);
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break ;
}
result--;
}
return result;
}
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
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++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
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++
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
cout << "gcd(10, 20) = " << __gcd(10, 20) << endl;
}
|
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
Share your thoughts in the comments
Please Login to comment...