GCD of Two Numbers in C++
Last Updated :
23 Jul, 2025
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.
Example:
Input: a = 12, b = 16
Output: 4
Explanation: As 4 is the largest number which divide both 12 and 16
Input: a = 11, b = 9
Output: 1
Explanation: As 1 is the largest number which divide both 11 and 9
Following are the different methods to find the GCD of two numbers in C++:
Simple Method
The idea is to find the common divisor by checking the divisibility of both numbers using the (%) modulo operator with all the numbers starting from the minimum number to 1.
Code Implementation
C++
// C++ program to find GCD of two numbers using simple
// arithmetic
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
// Find Minimum of a and b
int res = min(a, b);
// Testing divisiblity with all numbers starting from
// min(a, b) to 1
while (res > 1) {
// If any number divide both a and b, so we
// got the answer
if (a % res == 0 && b % res == 0)
break;
res--;
}
return res;
}
int main() {
int a = 12, b = 16;
// Finding gcd of two numbers a and b
cout << gcd(a, b);
return 0;
}
Time Complexity: O(min(a,b)), where a and b are the given two numbers.
Auxiliary Space: O(1)
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.
Code Implementation
C++
// C++ program to find GCD of two numbers using
// Euclidean Algorithm
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// If both numbers are equal
if (a == b)
return a;
// If a is greater
if (a > b)
return gcd(a - b, b);
// If b is greater
return gcd(a, b - a);
}
main() {
// Finding gcd of a and b
int a = 12, b = 16;
cout << gcd(a, b);
return 0;
}
Time Complexity: O(log (min(a,b)) ), where a and b are the given two numbers.
Auxiliary Space: O(log (min(a,b)) )
Using Library Functions
In C++ Standard Library, there are two standard library functions: __gcd() and gcd() which is used for calculating the GCD of two numbers but they are present since C++ 14 and C++ 17 standard only. __gcd() is defined inside <algorithm> header file and gdc() is defined inside <numeric> header file.
Syntax
__gcd(a, b) // Since C++ 14
gcd(a, b) // Since C++ 17
Parameters
- a: First number.
- b: Second number.
Return Value
- Return the GCD of given two numbers.
Code Implementation
C++
// C++ program to find gcd of two numbers using
// inbuilt __gcd() and gdc() function
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 12, b = 16;
// Finding gcd of a and b using __gcd()
cout << __gcd(a, b) << endl;
// Finding gcd of a and b using gcd()
cout << gcd(a, b);
return 0;
}
Output
4
4
Time Complexity: O(log n)
GCD of Two Numbers
GCD of Two Numbers
Similar Reads
Interview Preparation
Practice @Geeksforgeeks
Data Structures
Algorithms
Programming Languages
Web Technologies
Computer Science Subjects
Data Science & ML
Tutorial Library
GATE CS