GCD of Two Numbers in C++
Last Updated :
07 Mar, 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)
Similar Reads
Add Two Numbers in C++ Given two integers, the task is to add these integer number and print their sum in C++.ExamplesInput: a = 11, b = 9Output: 20Explanation: Sum of 11 + 9 = 20Input: a = 1, b = 8Output: 9Explanation: Sum of 1 + 8 = 9 Add Two Numbers Using Addition OperatorIn C++, the simplest method for adding the two
3 min read
C++ Program To Find LCM of Two Numbers LCM (Least Common Multiple) of two numbers is the smallest number that is divisible by both numbers. For example, the LCM of 15 and 20 is 60, and the LCM of 15 and 25 is 75. In this article, we will learn to write a C++ program to find the LCM of two numbers. We can find the LCM of two numbers in C+
4 min read
Swap Two Numbers using Function in C++ Swapping numbers means exchanging the values of the two numbers with each other. For Example, Before Swapping:a = 10, b = 22;After swapping:a = 22, b = 10In this article, we will write a program to swap two numbers using a function in C++. How to Swap Two Numbers Using Function in C++?We will pass t
3 min read
How to Handle Large Numbers in C++? In C++, when working with very large numbers the standard data types like int can become insufficient due to their limited range. In this article, we will learn how we can handle large numbers in C++. Handle Large Numbers in C++To store integers of different sizes, C++ offers built-in data types lik
3 min read
C++ Program for Common Divisors of Two Numbers Given two integer numbers, the task is to find the count of all common divisors of given numbers. Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b = 17 Output: 1 // all common divisors are 1 Input : a = 20, b = 36 Output: 3 // all common divisors a
2 min read