Python Program to find HCF and LCM | Find HCF and LCM of two numbers

Python Program to find HCF and LCM 

Python Program to find HCF and LCM
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) 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.


LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers. 
For example, LCM of 15 and 20 is 60, and LCM of 5 and 7 is 35.


Program to find GCD & LCM:
def compute_hcf(xy):
# choose the smaller number
    if x > y:
        smaller = y
    else:
        smaller = x
    for i in range(1, smaller+1):
        if((x % i == 0and (y % i == 0)):
            hcf = i 
    return hcf
num1 = int(input("enter num1: ")) 
num2 = int(input("enter num2: "))
print(f"The HCF of {num1} & {num2} is: ", compute_hcf(num1, num2))
lcm= ((num1*num2)//compute_hcf(num1, num2))
print(f"The LCM of {num1} & {num2} is: ", lcm)

Output:
enter num1: 15 enter num2: 20 The HCF of 15 & 20 is: 5 The LCM of 15 & 20 is: 60
enter num1: 20
enter num2: 28
The HCF of 20 & 28 is: 4
The LCM of 20 & 28 is: 140


Run Code- If you want run this code copy this code, paste here and run.

No comments: