Python Program to Find HCF and LCM


Python Program to Find HCF and LCM

  • To find HCF and LCM of two numbers in python, you have to ask from user to enter any two number to find and print the value of HCF and LCM as output as shown in the program given here.

Source Code:

print("Enter 'x' for exit.")
print("Enter two numbers to find HCF and LCM: ")
num1 = input()
if num1 == 'x':
    num2 = input()
    number1 = int(num1)
    number2 = int(num2)
    temp1 = number1
    temp2 = number2
    while temp2 != 0:
        t = temp2
        temp2 = temp1%temp2
        temp1 = t
    hcf = temp1
    lcm = (number1*number2)/hcf
    print("HCF =",hcf)
    print("LCM =",lcm) 


Post a Comment

Post a Comment (0)