GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them.

For example GCD of 20 and 28 is 4 and GCD of 98 and 56 is 14.
A simple solution is to find all prime factors of both numbers, then find intersection of all factors present in both numbers. Finally return product of elements in the intersection.
An efficient solution is to use Euclidean algorithm which is the main algorithm used for this purpose. The idea is, GCD of two numbers doesn’t change if smaller number is subtracted from a bigger number.
C++
// C++ program to find GCD of two numbers#include <iostream>using namespace std;// Recursive function to return gcd of a and bint 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 program to test above functionint main(){ int a = 98, b = 56; cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b); return 0;} |
C
// C program to find GCD of two numbers#include <stdio.h>// Recursive function to return gcd of a and bint 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 program to test above functionint main(){ int a = 98, b = 56; printf("GCD of %d and %d is %d ", a, b, gcd(a, b)); return 0;} |
Java
// Java program to find GCD of two numbersclass Test{ // Recursive function to return gcd of a and b static 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 method public static void main(String[] args) { int a = 98, b = 56; System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b)); }} |
Python3
# Recursive function to return gcd of a and bdef gcd(a,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 program to test above functiona = 98b = 56if(gcd(a, b)): print('GCD of', a, 'and', b, 'is', gcd(a, b))else: print('not found')# This code is contributed by Danish Raza |
C#
// C# program to find GCD of two// numbersusing System;class GFG { // Recursive function to return // gcd of a and b static 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 method public static void Main() { int a = 98, b = 56; Console.WriteLine("GCD of " + a +" and " + b + " is " + gcd(a, b)); }}// This code is contributed by anuj_67. |
PHP
<?php// PHP program to find GCD // of two numbers// Recursive function to // return gcd of a and bfunction gcd($a, $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$a = 98 ;$b = 56 ;echo "GCD of $a and $b is ", gcd($a , $b) ;// This code is contributed by Anivesh Tiwari?> |
Output:
GCD of 98 and 56 is 14
A more efficient solution is to use modulo operator in Euclidean algorithm .
C++
// C++ program to find GCD of two numbers#include <iostream>using namespace std;// Recursive function to return gcd of a and bint gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } // Driver program to test above functionint main(){ int a = 98, b = 56; cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b); return 0;} |
C
// C program to find GCD of two numbers#include <stdio.h>// Recursive function to return gcd of a and bint gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); }// Driver program to test above functionint main(){ int a = 98, b = 56; printf("GCD of %d and %d is %d ", a, b, gcd(a, b)); return 0;} |
Java
// Java program to find GCD of two numbersclass Test{ // Recursive function to return gcd of a and b static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Driver method public static void main(String[] args) { int a = 98, b = 56; System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b)); }} |
Python3
# Recursive function to return gcd of a and bdef gcd(a,b): # Everything divides 0 if (b == 0): return a return gcd(b, a%b)# Driver program to test above functiona = 98b = 56if(gcd(a, b)): print('GCD of', a, 'and', b, 'is', gcd(a, b))else: print('not found')# This code is contributed by Danish Raza |
C#
// C# program to find GCD of two// numbersusing System;class GFG { // Recursive function to return // gcd of a and b static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Driver method public static void Main() { int a = 98, b = 56; Console.WriteLine("GCD of " + a +" and " + b + " is " + gcd(a, b)); }}// This code is contributed by anuj_67. |
PHP
<?php// PHP program to find GCD // of two numbers// Recursive function to // return gcd of a and bfunction gcd($a, $b){ // Everything divides 0 if($b==0) return $a ; return gcd( $b , $a % $b ) ;}// Driver code$a = 98 ;$b = 56 ;echo "GCD of $a and $b is ", gcd($a , $b) ;// This code is contributed by Anivesh Tiwari?> |
Output:
GCD of 98 and 56 is 14
The time complexity for the above algorithm is O(log(min(a,b))) the derivation for this is obtained from the analysis of the worst-case scenario. What we do is we ask what are the 2 least numbers that take 1 step, those would be (1,1). If we want to increase the number of steps to 2 while keeping the numbers as low as possible as we can take the numbers to be (1,2). Similarly, for 3 steps, the numbers would be (2,3), 4 would be (3,5), 5 would be (5,8). So we can notice a pattern here, for the nth step the numbers would be (fib(n),fib(n+1)). So the worst-case time complexity would be O(n) where a>= fib(n) and b>= fib(n+1).
Now Fibonacci series is an exponentially growing series where the ratio of nth/(n-1)th term approaches (sqrt(5)-1)/2 which is also called the golden ratio. So we can see that the time complexity of the algorithm increases linearly as the terms grow exponentially hence the time complexity would be log(min(a,b))!
Please refer GCD of more than two (or array) numbers to find HCF of more than two numbers.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program to find GCD or HCF of two numbers using Middle School Procedure
- Find HCF of two numbers without using recursion or Euclidean algorithm
- Program to find HCF (Highest Common Factor) of 2 Numbers
- Program to find HCF iteratively
- HCF of array of fractions (or rational numbers)
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Smallest subsequence having GCD equal to GCD of given array
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
- Find the other number when LCM and HCF given
- Find two numbers whose sum and GCD are given
- Program to find GCD of floating point numbers
- Program to find LCM of 2 numbers without using GCD
- LCM and HCF of fractions
- Count the number of subsequences of length k having equal LCM and HCF
- Number of pairs such that their HCF and LCM is equal
- GCD of two numbers when one of them can be very large
- GCD of two numbers formed by n repeating x and y times
- GCD of more than two (or array) numbers
- Finding LCM of more than two (or array) numbers without using GCD

