Find LCM and HCF

 def find_lcm(num1, num2):

larger = max(num1, num2) while True: if larger % num1 == 0 and larger % num2 == 0: lcm = larger break larger += 1 return lcm def find_hcf(num1, num2): smaller = min(num1, num2) for i in range(smaller, 0, -1): if num1 % i == 0 and num2 % i == 0: return i # Taking input from the user num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) lcm = find_lcm(num1, num2) hcf = find_hcf(num1, num2) print("The LCM of", num1, "and", num2, "is", lcm) print("The HCF of", num1, "and", num2, "is", hcf) In this code, we define two functions: `find_lcm()` to calculate the LCM and `find_hcf()` to calculate the HCF. The `find_lcm()` function uses a `while` loop to check if the current value of `larger` is divisible by both `num1` and `num2`. If it is, then it is the LCM and the loop breaks. If it's not, `larger` is incremented by 1 and the loop continues until the LCM is found.

The `find_hcf()` function uses a `for` loop to iterate from the `smaller` number down to 1. It checks if the current value of `i` is a common factor of both `num1` and `num2`. If it is, that value is returned as the HCF.

Finally, we take the input from the user using the `input()` function and call the functions to calculate the LCM and HCF. The results are then printed using the `print()` function.

Comments

Popular posts from this blog

Ecommerce website

Yes, Python is an object-oriented programming (OOP) language, but it is also a multi-paradigm language

Your task is to find the missing number.