Skip to content
Home » Python code to find H.C.F. and L.C.M. of two numbers using functions

Python code to find H.C.F. and L.C.M. of two numbers using functions

Below is the python code to find H.C.F. and L.C.M. of two numbers using functions.

LCM= Product of the numbers/HCF

def HCF(n1,n2):
    a,b=n1,n2
    p=a*b
    for i in range(1,p):
        if (a%i==0) and (b%i==0):
            gcd=i
    return gcd
# main program
m=int(input("Enter first number:"))
n=int(input("Enter second number:"))
k=HCF(m,n)
lcm=int((m*n)/k)
print("The GCD/HCF of two numbers=",k)
print("The LCM of two numbers=",lcm)

Output

Enter first number:65

Enter second number:7

The GCD/HCF of two numbers= 1

The LCM of two numbers= 455