HCF (Highest Common Factor) or GCD (Greatest Common Divisor) 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
#include <stdio.h>
int gcd(int a, int b)
{
if (a == 0 && b == 0)
return 0;
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 = 0, b = 56;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}
|
Java
class Test {
static int gcd(int a, int b)
{
if (a == 0 && b == 0)
return 0;
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);
}
public static void main(String[] args)
{
int a = 98, b = 56;
System.out.println("GCD of " + a + " and " + b
+ " is " + gcd(a, b));
}
}
|
Python3
def gcd(a, b):
if(a == 0 and b == 0):
return 0
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)
a = 98
b = 56
if(gcd(a, b)):
print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
print('not found')
|
C#
using System;
class GFG {
static int gcd(int a, int b)
{
if (a == 0 && b == 0)
return 0;
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);
}
public static void Main()
{
int a = 98, b = 56;
Console.WriteLine("GCD of " + a + " and " + b
+ " is " + gcd(a, b));
}
}
|
PHP
<?php
function gcd($a, $b)
{
if($a==0 && $b==0)
return 0 ;
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 ) ;
}
$a = 98 ;
$b = 56 ;
echo "GCD of $a and $b is ", gcd($a , $b) ;
?>
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0 && b == 0)
return 0;
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);
}
var a = 98, b = 56;
document.write("GCD of " + a + " and " +
b + " is " + gcd(a, b));
</script>
|
Output:
GCD of 98 and 56 is 14
A more efficient solution is to use modulo operator in Euclidean algorithm .
C
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
int a = 98, b = 56;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}
|
Java
class Test
{
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void main(String[] args)
{
int a = 98, b = 56;
System.out.println("GCD of " + a +" and " + b +
" is " + gcd(a, b));
}
}
|
Python3
def gcd(a,b):
if (b == 0):
return a
return gcd(b, a%b)
a = 98
b = 56
if(gcd(a, b)):
print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
print('not found')
|
C#
using System;
class GFG {
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void Main()
{
int a = 98, b = 56;
Console.WriteLine("GCD of "
+ a +" and " + b + " is "
+ gcd(a, b));
}
}
|
PHP
<?php
function gcd($a, $b)
{
if($b==0)
return $a ;
return gcd( $b , $a % $b ) ;
}
$a = 98 ;
$b = 56 ;
echo "GCD of $a and $b is ", gcd($a , $b) ;
?>
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
var a = 98, b = 56;
document.write("GCD of " + a +" and " + b +
" is " + gcd(a, b));
</script>
|
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
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.