# Find HCF of two numbers without using recursion or Euclidean algorithm

Given two integer **x** and **y**, the task is to find the HCF of the numbers without using recursion or Euclidean method.

**Examples:**

Input:x = 16, y = 32

Output:16

Input:x = 12, y = 15

Output:3

**Approach:** HCF of two numbers is the greatest number which can divide both the numbers. If the smaller of the two numbers can divide the larger number then the HCF is the smaller number. Else starting from (smaller / 2) to 1 check whether the current element divides both the numbers . If yes, then it is the required HCF.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the HCF of x and y ` `int` `getHCF(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Minimum of the two numbers ` ` ` `int` `minimum = min(x, y); ` ` ` ` ` `// If both the numbers are divisible ` ` ` `// by the minimum of these two then ` ` ` `// the HCF is equal to the minimum ` ` ` `if` `(x % minimum == 0 && y % minimum == 0) ` ` ` `return` `minimum; ` ` ` ` ` `// Highest number between 2 and minimum/2 ` ` ` `// which can divide both the numbers ` ` ` `// is the required HCF ` ` ` `for` `(` `int` `i = minimum / 2; i >= 2; i--) { ` ` ` ` ` `// If both the numbers ` ` ` `// are divisible by i ` ` ` `if` `(x % i == 0 && y % i == 0) ` ` ` `return` `i; ` ` ` `} ` ` ` ` ` `// 1 divides every number ` ` ` `return` `1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 16, y = 32; ` ` ` `cout << getHCF(x, y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the HCF of x and y ` `static` `int` `getHCF(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Minimum of the two numbers ` ` ` `int` `minimum = Math.min(x, y); ` ` ` ` ` `// If both the numbers are divisible ` ` ` `// by the minimum of these two then ` ` ` `// the HCF is equal to the minimum ` ` ` `if` `(x % minimum == ` `0` `&& y % minimum == ` `0` `) ` ` ` `return` `minimum; ` ` ` ` ` `// Highest number between 2 and minimum/2 ` ` ` `// which can divide both the numbers ` ` ` `// is the required HCF ` ` ` `for` `(` `int` `i = minimum / ` `2` `; i >= ` `2` `; i--) ` ` ` `{ ` ` ` ` ` `// If both the numbers ` ` ` `// are divisible by i ` ` ` `if` `(x % i == ` `0` `&& y % i == ` `0` `) ` ` ` `return` `i; ` ` ` `} ` ` ` ` ` `// 1 divides every number ` ` ` `return` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `x = ` `16` `, y = ` `32` `; ` ` ` `System.out.println(getHCF(x, y)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the HCF of x and y ` `def` `getHCF(x, y): ` ` ` ` ` `# Minimum of the two numbers ` ` ` `minimum ` `=` `min` `(x, y) ` ` ` ` ` `# If both the numbers are divisible ` ` ` `# by the minimum of these two then ` ` ` `# the HCF is equal to the minimum ` ` ` `if` `(x ` `%` `minimum ` `=` `=` `0` `and` `y ` `%` `minimum ` `=` `=` `0` `): ` ` ` `return` `minimum ` ` ` ` ` `# Highest number between 2 and minimum/2 ` ` ` `# which can divide both the numbers ` ` ` `# is the required HCF ` ` ` `for` `i ` `in` `range` `(minimum ` `/` `/` `2` `, ` `1` `, ` `-` `1` `): ` ` ` ` ` `# If both the numbers are divisible by i ` ` ` `if` `(x ` `%` `i ` `=` `=` `0` `and` `y ` `%` `i ` `=` `=` `0` `): ` ` ` `return` `i ` ` ` ` ` `# 1 divides every number ` ` ` `return` `1` ` ` `# Driver code ` `x, y ` `=` `16` `, ` `32` `print` `(getHCF(x, y)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the HCF of x and y ` `static` `int` `getHCF(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Minimum of the two numbers ` ` ` `int` `minimum = Math.Min(x, y); ` ` ` ` ` `// If both the numbers are divisible ` ` ` `// by the minimum of these two then ` ` ` `// the HCF is equal to the minimum ` ` ` `if` `(x % minimum == 0 && y % minimum == 0) ` ` ` `return` `minimum; ` ` ` ` ` `// Highest number between 2 and minimum/2 ` ` ` `// which can divide both the numbers ` ` ` `// is the required HCF ` ` ` `for` `(` `int` `i = minimum / 2; i >= 2; i--) ` ` ` `{ ` ` ` ` ` `// If both the numbers ` ` ` `// are divisible by i ` ` ` `if` `(x % i == 0 && y % i == 0) ` ` ` `return` `i; ` ` ` `} ` ` ` ` ` `// 1 divides every number ` ` ` `return` `1; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `x = 16, y = 32; ` ` ` `Console.WriteLine(getHCF(x, y)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

= 2; $i–)

{

// If both the numbers

// are divisible by i

if ($x % $i == 0 &&

$y % $i == 0)

return $i;

}

// 1 divides every number

return 1;

}

// Driver code

$x = 16; $y = 32;

echo(getHCF($x, $y));

// This code is contributed

// by Code_Mech.

?>

**Output:**

16

## Recommended Posts:

- Find the value of ln(N!) using Recursion
- New Algorithm to Generate Prime Numbers from 1 to Nth Number
- Algorithm to generate positive rational numbers
- Pairs with same Manhattan and Euclidean distance
- Euclidean algorithms (Basic and Extended)
- C Program for Basic Euclidean algorithms
- Java Program for Basic Euclidean algorithms
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Find two numbers whose sum and GCD are given
- Given two numbers a and b find all x such that a % x = b
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find XOR of numbers from the range [L, R]
- Program to find LCM of 2 numbers without using GCD
- Find two prime numbers with given sum
- Find two numbers with sum and product both same as N

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.