Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
In mathematics, the concept of the Least Common Multiple (LCM) holds great significance. Whether you're tackling problems involving fractions, prime factorization, or solving equations with multiple variables, understanding the LCM becomes indispensable. In this tutorial, we will learn how to find LCM of two numbers in Python.
The LCM represents the smallest positive integer that is divisible by two or more given numbers without leaving any remainder. It serves as a crucial tool in various mathematical operations, such as simplifying fractions, comparing and combining fractions, and finding common denominators.
Moreover, it plays a vital role in solving real-world problems related to scheduling, time management, and resource allocation.
With the LCM program in Python, we will dive into different approaches to calculating the Least Common Multiple. To follow along with this tutorial in the best manner, you should have a basic understanding of Python programming.
If you are a beginner, it is recommended to start with our Python Tutorial or go for an expert-led Python Course.
So, here, we will be using built-in functions and mathematical operations to create our LCM program in Python. You can try the given example code on with an online Python compiler and practice it on your own.
LCM of Two Numbers in Python
Code
# Python program to find LCM of 2 numbers
def calculate_lcm(a, b):
# Find the maximum of the two numbers
max_num = max(a, b)
while True:
if max_num % a == 0 and max_num % b == 0:
lcm = max_num
break
max_num += 1
return lcm
# Example usage
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
lcm = calculate_lcm(num1, num2)
print(f"The LCM of {num1} and {num2} is: {lcm}")
Output
Output 1:
Enter the first number: 510
Enter the second number: 92
The LCM of 510 and 92 is: 23460
Output 2:
Enter the first number: 468
Enter the second number: 520
The LCM of 468 and 520 is: 4680
Explanation
-
We define a function called calculate_lcm that takes two parameters a and b, representing the two numbers for which we want to find the LCM.
-
Inside the function, we initialize a variable max_num with the maximum of a and b. We then use a while loop that continues until we find a number that is divisible by both a and b. We increment max_num by 1 in each iteration.
-
Once we find such a number, we assign it to the variable lcm and break out of the loop.
-
Finally, we print the calculated LCM.
LCM of Three Numbers in Python
Code
# Python program to find LCM of 3 numbers
def calculate_lcm(a, b, c):
# Find the maximum of the three numbers
max_num = max(a, b, c)
while True:
if max_num % a == 0 and max_num % b == 0 and max_num % c == 0:
lcm = max_num
break
max_num += 1
return lcm
# Example usage
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
lcm = calculate_lcm(num1, num2, num3)
print(f"The LCM of {num1}, {num2}, and {num3} is: {lcm}")
Output
Enter the first number: 48
Enter the second number: 72
Enter the third number: 108
The LCM of 48, 72, and 108 is: 432
Explanation
-
In this program, we define a function called calculate_lcm that takes three parameters a, b, and c, representing the three numbers for which we want to find the LCM.
-
Inside the function, we initialize a variable max_num with the maximum of a, b, and c. We then use a while loop that continues until we find a number that is divisible by all three numbers. We increment max_num by 1 in each iteration.
-
Once we find such a number, we assign it to the variable lcm and break out of the loop.
-
Finally, we return the calculated LCM of three numbers in Python.
LCM of List in Python
Code
# Python program to find LCM of a list of numbers
def calculate_lcm(numbers):
# Find the maximum number in the list
max_num = max(numbers)
lcm = max_num
while True:
if all(lcm % num == 0 for num in numbers):
break
lcm += max_num
return lcm
# Example usage
numbers = [2, 3, 4, 5, 6]
lcm = calculate_lcm(numbers)
print(f"The LCM of {numbers} is: {lcm}")
Output
The LCM of [2, 3, 4, 5, 6] is: 60
Explanation
-
In this program, we define a function called calculate_lcm that takes a list of numbers as the parameter.
-
Inside the function, we find the maximum number in the list using the max() function and assign it to max_num. We initialize the lcm variable with the maximum number.
-
We use a while loop that continues until we find a number (lcm) that is divisible by all the numbers in the list. We use the all() function along with a generator expression to check if lcm is divisible by each number in the list.
-
Once we find such an lcm, we break out of the loop and return the calculated LCM.
LCM in Python Using for loop
Here's the program to find the least common multiple (LCM) of two numbers using a for loop in Python:
Code
def find_lcm(num1, num2):
# Find the maximum of the two numbers
max_num = max(num1, num2)
lcm = 0
# Start a loop from the maximum number
for i in range(max_num, num1 * num2 + 1, max_num):
# Check if the current number is divisible by both num1 and num2
if i % num1 == 0 and i % num2 == 0:
lcm = i
break
return lcm
# Test the function
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
result = find_lcm(number1, number2)
print("The least common multiple of", number1, "and", number2, "is", result)
Output
Enter the first number: 66
Enter the second number: 45
The least common multiple of 66 and 45 is 990
Explanation
-
In this code, the find_lcm function takes two numbers as input (num1 and num2). It starts a loop from the maximum of the two numbers and checks each number to see if it is divisible by both num1 and num2.
-
Once it finds the first number that satisfies this condition, it assigns it to the variable lcm and breaks out of the loop.
-
Finally, the function returns the value of lcm. The user is prompted to enter two numbers, and the function is called with these numbers as arguments. The result is then printed.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program