Open In App

Program to find HCF iteratively

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

HCF (Highest Common Factor) or GCD (Greatest Common Divisor) of two numbers is the largest number that divides both of them. 

GCDFor example, GCD of 20 and 28 is 4, and GCD of 98 and 56 is 14.

Approach 1:
We have discussed the recursive solution in the below post. Recursive program to find GCD or HCF of two numbers

Below is the iterative implementation of Euclid’s algorithm 

C++




// 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 code
int main()
{
    int a = 60, b = 96;
    cout << hcf(a, b) << endl;
    return 0;
}
 
// This code is contributed by shubhamsingh10


C




// 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




// JAVA Code for Program to find
// HCF iteratively
import 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.


Python3




#Python program to find HCF of two numbers iteratively.
 
def hcf(a, b):
    if a == 0:
        return b
    elif b == 0:
        return a
    while a != b:
        if a > b:
            a = a - b
        else:
            b = b - a
    return a
 
 
a = 60
b = 96
print(hcf(a, b))


C#




// C# Code for Program to find
// HCF iteratively
using 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.


Javascript




<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>


PHP




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


Output

12

Time Complexity: O(max(a, b))
Auxiliary Space: O(1)

Approach 2: To find the highest common factor (HCF) of two numbers “a” and “b”, start with the input values. Enter a loop while “b” is not Zero. Store “b” in a temporary variable “temp”, Update “b” to be the remainder of “a” divided by “b” (b = a % b), Update “a” with “temp”. Repeat until “b” becomes Zero. Return the value of “a” as the HCF of the original “a” and “b”.

Below is the implementation of Euclid’s algorithm

C++




#include <iostream>
using namespace std;
 
// Function to find the HCF (GCD) of two numbers iteratively
int findHCF(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
// Nikunj Sonigara
// Driver code
int main() {
    int num1 = 60, num2 = 96;
 
    int hcf = findHCF(num1, num2);
    cout << "HCF of " << num1 << " and " << num2 << " is " << hcf << endl;
 
    return 0;
}


Java




public class Main {
    // Function to find the HCF (GCD) of two numbers
    // iteratively
    public static int findHCF(int a, int b)
    {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
 
    public static void main(String[] args)
    {
        int num1 = 60, num2 = 96;
 
        int hcf = findHCF(num1, num2);
        System.out.println("HCF of " + num1 + " and " + num2
                           + " is " + hcf);
    }
}


Python3




# Function to find the HCF (GCD) of two numbers iteratively
def findHCF(a, b):
    while b != 0:
        temp = b
        b = a % b
        a = temp
    return a
 
# Driver code
if __name__ == "__main__":
    num1 = 60
    num2 = 96
 
    hcf = findHCF(num1, num2)
    print(f"HCF of {num1} and {num2} is {hcf}")


C#




using System;
 
class Program {
    // Function to find the HCF (GCD) of two numbers
    // iteratively
    static int FindHCF(int a, int b)
    {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
 
    static void Main()
    {
        int num1 = 60, num2 = 96;
 
        int hcf = FindHCF(num1, num2);
        Console.WriteLine(
            $"HCF of {num1} and {num2} is {hcf}");
    }
}


Javascript




// Function to find the HCF (GCD) of two numbers iteratively
function findHCF(a, b) {
    while (b !== 0) {
        let temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
 
// Driver code
const num1 = 60;
const num2 = 96;
 
const hcf = findHCF(num1, num2);
console.log(`HCF of ${num1} and ${num2} is ${hcf}`);


Output

HCF of 60 and 96 is 12

Time Complexity: O(log(min(a, b)))
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads