Python program to find HCF of Two Numbers

HCF of Two Numbers

Here, in this section we will discuss how to find HCF of two numbers in python. HCF means (Highest Common Factor) also known as GCD (Greatest Common Divisor).

x is called HCF of a & b two conditions :

  • x can completely divide both a & b leaving remainder 0
  • No, other number greater than x can completely divide both a & b
virtual in Python

What's on the Page

  • Method 1: Linear Quest to find HCF
  • Method 2: Euclidean Algorithm: Repeated Subtraction
  • Method 3: Recursive Euclidean Algorithm: Repeated Subtraction
  • Method 4: Modulo Recursive Euclidean Algorithm: Repeated Subtraction
  • Method 5: Handling Negative Numbers in HCF

Method 1 : Linear Quest

Algorithm

  • Initialize HCF = 1
  • Run a loop in the iteration of (i) between [1, min(num1, num2)]
  • Note down the highest number that divides both num1 & num2
  • If i satisfies (num1 % i == 0 and num2 % i == 0) then new value of HCF is i
  • Print value of HCF

Method 1 : Python Code

Run
num1 = 36
num2 = 60
hcf = 1

for i in range(1, min(num1, num2)):
    if num1 % i == 0 and num2 % i == 0:
        hcf = i
print("Hcf of", num1, "and", num2, "is", hcf)

Output

HCF of 36 and 60 is 12

Method 2 : Repeated Subtraction

Algorithm

  • Run a while loop until num1 is not equals to num2
  • If num1>num2 then num1 = num1 – num2
  • Else num2 = num2 – num1
  • After the loop ends both num1 & num2 stores HCF

Method 2 : Python Code

Run
num1 = 36
num2 = 60
a = num1
b = num2

while num1 != num2:
    if num1 > num2:
        num1 -= num2
    else:
        num2 -= num1

print("Hcf of", a, "and", b, "is", num1)

Output

HCF of 36 and 60 is 12

Method 3 : Repeated Subtraction using Recursion

Algorithm

  • Checked whether any of the input is 0 then return sum of both numbers
  • If both input are equal return any of the two numbers
  • If num1 is greater than the num2 then Recursively call findHCF(num1 – num2, num2)
  • Else Recursively call findHCF(num1, num2-num1)

Method 3 : Python Code

Run
# Recursive function to return HCF of two number
def findHCF(num1, num2):
    
    # Everything divides 0
    if num1 == 0 or num2 == 0:
        return num1 + num2
    
    # base case
    if num1 == num2:
        return num1
    
    # num1>num2
    if num1 > num2:
        return findHCF(num1 - num2, num2)
    else:
        return findHCF(num1, num2 - num1)


num1 = 36
num2 = 60

print("Hcf of", num1, "and", num2, "is", findHCF(num1, num2))

Output

HCF of 36 and 60 is 12

Method 4 : Repeated Subtraction with Modulo Operator using Recursion

Algorithm

  • If b is equals to 0 return a
  • Else recursively call the function for value b, a%b and return 

Method 4 : Python Code

Run
# This method improves complexity of repeated subtraction
# By efficient use of modulo operator in euclidean algorithm
def getHCF(a, b):
    return b == 0 and a or getHCF(b, a % b)


num1 = 36
num2 = 60

print("Hcf of", num1, "and", num2, "is", getHCF(num1, num2))

Output

HCF of 36 and 60 is 12

Method 5 : Handling Negative Numbers in HCF

Algorithm

If any of the number is negative then convert it to positive by multiplying it with -1 as according to the proper definition HCF of two numbers can never be negative.

  • If b is equals to 0 return a
  • Else recursively call the function for value b, a%b and return 

Method 5 : Python Code

Run
# This method improves complexity of repeated subtraction
# By efficient use of modulo operator in euclidean algorithm
def getHCF(a, b):
    return b == 0 and a or getHCF(b, a % b)


num1 = -36
num2 = 60

# if user enters negative number, we just changing it to positive
# By definition HCF is the highest positive number that divides both numbers
# -36 & 60 : HCF = 12 (as highest num that divides both)
# -36 & -60 : HCF = 12 (as highest num that divides both)
num1 >= 0 and num1 or -num1
num2 >= 0 and num2 or -num2

print("Hcf of", num1, "and", num2, "is", getHCF(num1, num2))

Output

HCF of -36 and 60 is 12

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

15 comments on “Python program to find HCF of Two Numbers”


  • GSaiCharan

    a=int(input(“enter:”))
    b=int(input(“enter:”))
    c=[]
    d=[]
    for i in range(1,a):
    if a%i==0:
    c.append(i)
    f=c
    print(f)
    for j in range(1,b):
    if b%j==0:
    d.append(j)
    e=d
    print(e)
    h=[]
    for k in f:
    for l in e:
    if k==l:
    h.append(k)
    x=[]
    for s in h:
    if a%s==0 and b%s==0:
    x.append(s)
    print(max(x))


  • D

    n1,n2=list(map(int,input().split()))
    hcf=1
    if n1<n2:
    l=n1
    else:
    l=n2
    for i in range(1,l):
    if n1%i==0 and n2%i==0:
    hcf=i
    print("hcf of ",n1,n2,'is',hcf)


  • Asazad

    ls=list(map(int,input().split()))
    g=1
    n=2
    f=0
    while(n<1000):
    for i in ls:
    if(i%n!=0):
    f=0
    n+=1
    break
    else:
    f=1
    if(f==1):
    r=[]
    for i in ls:
    r.append(i/n)
    ls=r
    g*=n
    f=0

    print(g)


  • NAGA

    program for finding HCF
    b=[]
    a=60
    for i in range(1,a+1):
    if a%i==0:
    b.append(i)

    c=[]
    d=40
    for j in range(1,a+1):
    if d%j==0:
    c.append(j)

    b=set(b)
    c=set(c)
    print(c&b)
    print(“highest common factor is :”,max(c&b))


  • liveforfriends1610

    x=int(input(“Enter x value: “))
    y=int(input(“Enter y value: “))
    while x!=y:
    if x>y:
    x=x-y
    else:
    y=y-x
    print(“THe HCF of x and y is: “,x)
    This’s also another way to find the GCD of two numbers


  • Pulla

    n1=int(input())
    n2=int(input())
    for i in range(1 ,min(n1,n2)+1):
    if n1%i ==0 and n2%i==0:
    output=i
    print(output)


  • PRAGADA

    a=int(input())
    b=int(input())
    c=min(a,b)
    For i in range(1,c+1):
    d=0
    if(a%i==0 & b%i==0)
    d=max(i,d)
    print(d)


  • dwivedisatyam1313

    n1 = int(input(“enter num1… “))
    n2 = int(input(“enter num2…. “))
    div1 =[]
    div2 =[]
    for i in range (1,n1+1):
    if n1%i == 0:
    div1.append(i)

    for j in range(1, n2 + 1):
    if n2 % j == 0:
    div2.append(j)
    cf =[]
    for i in div1:
    for j in div2:
    if i == j:
    cf.append(i)
    cf.sort()
    print(f”the HCF of {n1} and {n2} is {cf[-1]}”)
    print(f”div1 = {div1}”)
    print(f”div2 = {div2}”)


  • Sonal

    num1=int(input(“Enter the 1st number “))
    num2=int(input(“Enter the 2nd number “))
    l=[]
    n=min(num1,num2)+1
    for i in range(1,n):
    if num1%i==0 and num2%i==0:
    l.append(int(i))
    else:
    continue
    hcf=max(l)
    print(“{} is the HCF of {} and {}”.format(hcf,num1,num2))


  • Nikitha

    first=int(input(“first number:”))
    second=int(input(“second number:”))
    if first<second:
    smaller=first
    else:
    smaller=second
    for i in range(1,smaller+1):
    if(first%i==0)and(second%i==0):
    hcf=i
    print("HCF of {} and {} is {}".format(first, second, hcf))


  • Prateek

    # GCD using Euclid Method

    def gcd(num1,num2):
    if num2 == 0:
    return num1
    return gcd(num2, num1%num2)

    num1 = int(input(“Enter a number1 “))
    num2 = int(input(“Enter a number2 “))

    print(“GCD of {} and {} = {}”.format(num1,num2,gcd(num1,num2)))


  • Karan

    a = int(input(“Enter your first number here: “))
    b = int(input(“Enter your second number here: “))
    c = abs(a-b)

    for i in range(c+1,1, -1):
    if a%i == b%i == 0:
    print(“The HCF of the given numbers is {}”.format(i))
    break