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

`// 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 && b == 0)` ` ` `return` `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 = 0, b = 56;` ` ` `printf` `(` `"GCD of %d and %d is %d "` `, a, b, gcd(a, b));` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## 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` `&& b == ` `0` `)` ` ` `return` `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));` ` ` `}` `}` |

*chevron_right*

*filter_none*

## Python3

`# Recursive function to return gcd of a and b` `def` `gcd(a, b):` ` ` `# Everything divides 0` ` ` `if` `(a ` `=` `=` `0` `and` `b ` `=` `=` `0` `):` ` ` `return` `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 ` `=` `98` `b ` `=` `56` `if` `(gcd(a, b)):` ` ` `print` `(` `'GCD of'` `, a, ` `'and'` `, b, ` `'is'` `, gcd(a, b))` `else` `:` ` ` `print` `(` `'not found'` `)` `# This code is contributed by Danish Raza` |

*chevron_right*

*filter_none*

## 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 && b == 0)` ` ` `return` `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.` |

*chevron_right*

*filter_none*

## 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 && ` `$b` `==0)` ` ` `return` `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` `?>` |

*chevron_right*

*filter_none*

**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 <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;` `}` |

*chevron_right*

*filter_none*

## 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));` ` ` `}` `}` |

*chevron_right*

*filter_none*

## 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 ` `=` `98` `b ` `=` `56` `if` `(gcd(a, b)):` ` ` `print` `(` `'GCD of'` `, a, ` `'and'` `, b, ` `'is'` `, gcd(a, b))` `else` `:` ` ` `print` `(` `'not found'` `)` `# This code is contributed by Danish Raza` |

*chevron_right*

*filter_none*

## 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.` |

*chevron_right*

*filter_none*

## 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` `?>` |

*chevron_right*

*filter_none*

**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 DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print the kth common factor of two numbers
- Program to find GCD or HCF of two numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure
- Find HCF of two numbers without using recursion or Euclidean algorithm
- Nearest element with at-least one common prime factor
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Program to find HCF iteratively
- Find the highest occurring digit in prime numbers in a range
- HCF of array of fractions (or rational numbers)
- Highest power of 2 that divides the LCM of first N Natural numbers.
- Find the other number when LCM and HCF given
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- 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
- Program to find the common ratio of three numbers
- Exactly n distinct prime factor numbers from a to b
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Count all the numbers in a range with smallest factor as K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.