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 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 program to test above function int 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 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 program to test above function int 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 numbers class 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 b def 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 function a = 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 // numbers using 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 b function 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 b int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Driver program to test above function int 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 b int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Driver program to test above function int 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 numbers class 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 b def gcd(a,b): # Everything divides 0 if (b == 0): return a return gcd(b, a%b) # Driver program to test above function a = 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 // numbers using 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 b function 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
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
Recommended Posts:
- Program to find LCM of 2 numbers without using GCD
- Program to find LCM of two numbers
- Program to find sum of first n natural numbers
- Program to find first N Iccanobif Numbers
- Program to find sum of prime numbers between 1 to n
- Program to find the percentage of difference between two numbers
- C program to Find the Largest Number Among Three Numbers
- Program to find GCD of floating point numbers
- Program to find the common ratio of three numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure
- Program to find HCF (Highest Common Factor) of 2 Numbers
- Program to find count of numbers having odd number of divisors in given range
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Program to Add Two Complex Numbers
- Program for Perrin numbers



