ScanSkill

LCM and HCF of Two Numbers in Python

In this example, you’ll get to learn how to find and prints the LCM and HCF of two numbers in Python entered by the user.

This covers the following:

  • Find LCM of Two Numbers in Python
  • Find HCF (GCD) of Two Numbers in Python
  • Find LCM and HCF of Two Numbers using Formula in Python

Note: LCM stands for Least Common Multiple. Whereas HCF stands for Highest Common Factor also known as GCD.

Prerequisites

Find LCM of Two Numbers in Python

To find the LCM of two numbers in Python, you have to ask from user to enter any two numbers, then find and print the LCM value.

print("Enter Two Numbers: ")
num1 = int(input())
num2 = int(input())

if num1 > num2:
    lcm = num1
else:
    lcm = num2

while True:
    if lcm%num1 == 0 and lcm%num2 == 0:
        break
    else:
        lcm = lcm + 1

print("The LCM of {num1} and {num2} = {lcm}")

Here, when the user passes two numbers, 10 as the first number and 25 as the second number by pressing ENTER, then the LCM of 10 and 25 will be printed as 50.

Explanation:

  • First, the numbers, num1=10 and num2=20 — entered by user are evaluated with the condition num1 > num2. If the condition is true, the lcm variable is initialized as num1 otherwise initialized as num2. In our condition lcm = 20.
  • Now, the while True condition is applied so that the program run inside the while loop if it’s satisfies the condition True.
  • Inside While loop, if lcm%num1 == 0 and lcm%num2 == 0 is applied with else condition, so that the program can evaluate as:
    • First condition lcm%num1 == 0 i.e. 25%10 == 0 or 5 == 0, which is false and second condition lcm%num2 == 0 i.e. 25%25 == 0 or 0 == 0, which is true.
    • Since lcm%num1 == 0 and lcm%num2 == 0 condition is false, else condition is executed i.e. value of the variable lcm will be lcm + 1 or 25+1 = 26.
    • Again while loop is run as the condition is not satisfied. Once the value of lcm becomes 50, then lcm%num1 == 0 i.e. 50%10 == 0 or 0 == 0 which is true and lcm%num2 == 0 i.e. 50%25 == 0 or 0 == 0 which is also true. Then the while loop is stopped as the condition is satisfied.
    • And the final output will be 50.

Also, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.

And, ==(is equal) assignment operator checks if the value of ch is equal to the mentioned value.

Output:

Enter Two Numbers: 
10
25
The LCM of 10 and 25 = 50

Find HCF (GCD) of Two Numbers in Python

This program is to find and print the value of HCF or GCD of two numbers entered by user:

print("Enter Two Numbers: ")
num1 = int(input())
num2 = int(input())

if num1>num2:
    hcf = num1
else:
    hcf = num2

while True:
    if num1%hcf==0 and num2%hcf==0:
        break
    else:
        hcf = hcf - 1

print(f"The HCF of {num1} and {num2} = {hcf}")

Here, the process is similar to evaluating LCM.

Output:

Enter Two Numbers: 
10
25
The HCF of 10 and 25 = 5

Find LCM and HCF using Formula in Python

This program uses formula to find and print LCM and HCF both in a single program:

print("Enter Two Numbers: ")
num1 = int(input())
num2 = int(input())

a = num1
b = num2
while b!=0:
    temp = b
    b = a%b
    a = temp

hcf = a
lcm = int((num1*num2)/hcf) # result as int type

print(f"The LCM of {num1} and {num2} = {lcm}")
print(f"The HCF of {num1} and {num2} = {hcf}")

Output:

Enter two numbers: 
10
25
The LCM of 10 and 25 = 50
The HCF of 10 and 25 = 5

Conclusion

In this, we discussed how to find LCM and HCF of two numbers using Python programming language using if else statement, while loop and formula.