Program to find HCF iteratively - GeeksforGeeks

26 Nov, 2021
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. 
We have discussed the recursive solution in the below post. Recursive program to find GCD or HCF of two numbersBelow is the iterative implementation of Euclid’s algorithm  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to find HCF of two// numbers iteratively.#include <bits/stdc++.h>using namespace std; int hcf(int a, int b){    if (a == 0)        return b;    else if (b == 0)        return a;    while (a != b) {        if (a > b)            a = a - b;        else            b = b - a;    }    return a;} // Driver codeint main(){    int a = 60, b = 96;    cout << hcf(a, b) << endl;    return 0;} // This code is contributed by shubhamsingh10
// C program to find HCF of two// numbers iteratively.#include <stdio.h> int hcf(int a, int b){    if (a == 0)        return b;    else if (b == 0)        return a;    while (a != b) {        if (a > b)            a = a - b;        else            b = b - a;    }    return a;}int main(){    int a = 60, b = 96;    printf("%d\n", hcf(a, b));    return 0;}
// JAVA Code for Program to find// HCF iterativelyimport java.util.*; class GFG {     static int hcf(int a, int b)    {        if (a == 0)            return b;        else if (b == 0)            return a;        while (a != b) {            if (a > b)                a = a - b;            else                b = b - a;        }        return a;    }     /* Driver program */    public static void main(String[] args)    {        int a = 60, b = 96;        System.out.println(hcf(a, b));    }} // This code is contributed by Arnav Kr. Mandal.
// Python program to find HCF of two// numbers iteratively.  def hcf(a, b):    if(a == 0):      return b    else if(b == 0):      return a    while (a != b):        if (a > b):            a = a - b        else:            b = b - a    return a  a = 60b = 96print(hcf(a, b))
// C# Code for Program to find// HCF iterativelyusing System; class GFG {     static int hcf(int a, int b)    {        if (a == 0)            return b;        else if (b == 0)            return a;        while (a != b) {            if (a > b)                a = a - b;            else                b = b - a;        }        return a;    }     // Driver program    public static void Main()    {        int a = 60, b = 96;        Console.WriteLine(hcf(a, b));    }} // This code is contributed by vt_m.
<?php//PHP program to find HCF of two// numbers iteratively. function hcf($a, $b){   if($a==0)      return $b;   else if($b==0)      return $a;    while ($a != $b) {        if ($a > $b)                $a = $a - $b;            else            $b = $b - $a;        }         return $a;} // Driver code$a = 60; $b = 96;echo hcf($a, $b), "\n";     // This code is contributed by ajit?>
<script> //Javascript program to find HCF of two// numbers iteratively. function hcf(a, b){   if (a == 0)        return b;    else if (b == 0)        return a;    while (a != b)    {        if (a > b)                a = a - b;            else               b = b - a;        }    return a;} // Driver code    let a = 60, b = 96;    document.write(hcf(a, b) + "<br>");     // This code is contributed by Mayank Tyagi </script>
Output: 
12
Time Complexity: O(max(a, b))
Auxiliary Space: O(1)
jit_t
SHUBHAMSINGH10
mayanktyagi1709
kajal mohite
samim2000
GCD-LCM
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Find all factors of a natural number | Set 1
Modulo 10^9+7 (1000000007)
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Operators in C / C++
Minimum number of jumps to reach end