LCM of two numbers in Python using functions
How to Find Least Common Multiple (LCM) in Python programming language by using the functions?
What is LCM?
- LCM stands for Least Common Multiple/ Lowest Common Multiple.
- LCM is a theory of arithmetic and number system.
How to demote LCM of two numbers?
The LCM of two numbers X and Y can be denoted by LCM (X,Y).
In this example, LCM is the lowest positive number that is divisible by both “X” and “Y”.
Example of LCM
We have two integers 1 and 2. Let’s find LCM
Multiples of 1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ………and many similar.
Multiples of 2: 2, 4, 6, 8, 10, 12, 14, 16,………and many similar.
Common multiples of 1 and 2 are will be the numbers that are common in both of these.
2, 4, 6,8, 10, ………and many similar.
LCM is the lowest common multiplier so, in this example, we have 2 as an LCM.
Write a Python program to find LCM of two numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Write a Python function to calculate LCM def LCM_T4Tutorials(x, y): # Checking for Big number if x > y: Greater_Number = x else:
Greater_Number = y while(True): if((Greater_Number % x == 0) and (Greater_Number %
y == 0)): lcm = Greater_Number break Greater_Number += 1 return lcm # Getting input from the users num1 = int(input("Please! Enter the first number: ")) num2 = int(input("Please! Enter the second number: ")) # Displaying the result for the users print("The L.C.M. of", num1,"and", num2,"is", LCM_T4Tutorials(num1, num2)) |
Output
Please! Enter the first number: 4
Please! Enter the second number: 5
The L.C.M. of 4 and 5 is 20