If you are like most developers, you are probably familiar with the popular programming language, Python. However, have you ever considered using this language to calculate LCM? The smallest multiple that two or more numbers have in common is defined as the least common multiple or LCM.

The least common multiple (LCM) is a mathematical convention used to calculate the greatest sum of two numbers that are not divisible. So, for example, if you have three students in your class and want to find out which one has the smallest GPA, then you would use the LCM.

This article will show you how to use **Python Program to find LCM** of two numbers. This common task is used in many different contexts, such as finance, engineering, and math. By using this Python program, you will be able to save time and get accurate results every time. Let us get started.

Table of Contents

## What is LCM in Python?

LCM stands for least common multiple; the LCM value helps optimize the quantities of the given objects. In Python, it is a mathematical operation used to find the smallest possible value, a multiple of two or more numbers. Also, in computer science, the LCM of numbers helps design encoded messages using cryptography.

### Algorithm

- Declare two numbers whose lcm want to find, a and b
- Check if a is greater than b , a>b If a>b greater = a Else greater= b
- Declare that the calculated multiples of the given numbers as , value=greater
- Set while as true
- Check, if greater a %==0 && greater b%==0
- After checking the above condition, Print the lcm of a and b is greater, break the loop.
- Else greater=greater+value

Also learn about, Python program to find GCD of two numbers.

## Python Program to Find LCM: 2 Methods

In the following code example, we have calculated the least common multiple of two numbers by taking from a user, calculating their multiples, and printing the least common of them.

```
def compute_lcm(a, b):
if a > b:
greater = a
else:
greater = b
while(True):
if((greater % a == 0) and (greater % b == 0)):
lcm = greater
break
greater += 1
return lcm
n1=int (input("enter any number:"))
n2=int (input("enter any number:"))
print("The L.C.M. is", compute_lcm(n1, n2))
```

#### Output

## 1. LCM using GCD

The Greatest common divisor (GCD) is a mathematical function that calculates the highest common factor of two numeric values. It can be used to find the greatest number that divides evenly into both inputs. It can help solve problems involving integers, and it plays an important role in various fields such as finance, mathematics, and physics.

LCM by the GCD method of two positive integers is used to find the least common multiple that divides the numbers without a remainder. It is useful for reducing fractions to be in their lowest terms.

To understand this method consider the following code snippet,

```
def compute_gcd(a, b):
while(b):
a, b = b, a % b
return a
def compute_lcm(a, b):
lcm = (a*b)//compute_gcd(a,b)
return lcm
n1=int (input("enter any number:"))
n2=int (input("enter any number:"))
print("The L.C.M. is", compute_lcm(n1, n2))
```

#### Output

## 2. LCM using Math Module Function

A math module in Python is a library which provides various mathematical functions that can be used within your code. This makes it easy to perform complex operations without having to clutter up your script with unnecessary code. Additionally, this helps to make your scripts more readable and organized.

There are several different method to calculate the lcm in Python by using math module function

### Using math.lcm() Function

The math.lcm function is a built-in method of math module function that returns the Least Common Multiple (LCM) of the integers.

It takes numbers as an argument and calculates the lcm of the given numbers.

To better understand, let us look at the example below

```
import math
print(math.lcm(2,3,4))
```

#### Output

### Using ma.gcd() Function

Gcd() function is the method that is used to find the greatest common divisor, and through this, we have calculated the least common multiple of the numbers taken from the user,

Consider the below code example to understand this method better

```
import math as ma
a=int (input("enter any number:"))
b=int (input("enter any number:"))
lcm = (a*b)/ma.gcd(a,b)
print("LCM of", a, "and", b, "is:", lcm)
```

#### Output

### Using ma.lcm() Function

Math module function also has an lcm() method. Simply by calling this method, it calculates the least common multiple of the numbers that the user gives,

Consider the below code snippet:

```
import math as ma
a=int (input("enter any number:"))
b=int (input("enter any number:"))
lcm = ma.lcm(a, b)
print("LCM of", a, "and", b, "is:", lcm)
```

#### Output

## 3. LCM using Iterative Statements

Iterative statements are a type of sentence that allows for multiple iterations to reach a final goal. For example, they can be used when making decisions, evaluating data, or programming code. This helps ensure that the results are accurate and efficient while allowing for changes if necessary.

There are multiple ways to calculate lcm by iterative methods explained in detail below

### Using For Loop

A for loop statement is a programming construct that allows you to repeat a set of code multiple times. It is commonly used in the context of variables but can also be used with functions and methods.

In the following code, we have calculated the lcm of two numbers by using for loop statement. Let us consider,

```
a=int (input("enter any number:"))
b=int (input("enter any number:"))
if a > b:
a, b = b, a
for i in range(1,a+1):
if a%i == 0 and b%i == 0:
gcd = i
lcm = (a*b)/gcd
print("LCM of", a, "and", b, "is:", lcm)
```

#### Output

### Using While Loop

A while loop statement is a type of conditional repetition in programming. It allows you to run a block of code multiple times as long as the condition specified within the loop remains true. This can be useful for making repetitive tasks easier to manage and more efficient.

Consider the following code example, here we calculate the lcm of the numbers by using while loop statement

```
a=int (input("enter any number:"))
b=int (input("enter any number:"))
while a != b:
if a > b:
a = a - b
else:
b = b - a
lcm = (a*b)/a
print("LCM of", a, "and", b, "is:", lcm)
```

#### Output

## 4. LCM using Recursive Function

A *recursive function* is a mathematical procedure that assigns the result of a given function to itself multiple times. This can be used to calculate functions’ specific properties, such as their maximum and minimum values. For example, recursive functions are often used in computer programming and statistics because they allow us to optimize our code or data by doing repeated calculations with known inputs.

We can also compute the lcm of numbers by a recursive function. Let us look at the below code snippet,

```
def gcd(a, b):
if b == 0:
return a
return gcd(b, a%b)
a=int (input("enter any number:"))
b=int (input("enter any number:"))
lcm = (a*b)/gcd(a,b)
print("LCM of", a, "and", b, "is:", lcm)
```

#### Output

## FAQs

### What is the importance of LCM?

L.C.M of two or more numbers help speed up problem-solving and save time. L.C.M. stands for least common factor that helps in Problem-solving in the areas of racing tracks, traffic lights, and so on.

### Why LCM is used in real life?

- When all the strings are glowing at the same time LCM = 6 is the answer for all strings that have timings between 1 and 3 seconds. As a result, all of the strings will shine simultaneously at the sixth second.
- A traffic controller analyses the timings of nearby places in the same region and adjusts the LCM of all traffic signals to determine when each signal should be green. By setting alerts at various times or increasing/decreasing the length of each movement, he may easily regulate traffic this manner.

### Can we find LCM of negative numbers?

Negative numbers are tricky to work with because they do not quite fit into the standard mathematical operations. However, there is a way to find LCM of negative numbers. The only difference between negative and positive numbers is that the LCM of negative numbers is a negative, and a positive number is positive.

## Conclusion

This article discussed how to implement **Python Program to find lcm** by using different methods and functions. When you start using Python for your day-to-day tasks, do not forget to keep an eye out for LCM cases. Keep reading to learn more.