Python Program to Find HCF or GCD

 # Python program to find H.C.F of two numbers


# define a function

def compute_hcf(x, y):


# choose the smaller number

    if x > y:

        smaller = y

    else:

        smaller = x

    for i in range(1, smaller+1):

        if((x % i == 0) and (y % i == 0)):

            hcf = i 

    return hcf


num1 = 54 

num2 = 24


print("The H.C.F. is", compute_hcf(num1, num2))

Source Code: Using the Euclidean Algorithm

# Function to find HCF the Using Euclidian algorithm
def compute_hcf(x, y):
   while(y):
       x, y = y, x % y
   return x

hcf = compute_hcf(300, 400)
print("The HCF is", hcf)

Output

The H.C.F. is 6

0 Comments