April 5, 2022, Learn eTutorial

1298

In this simple python program, we need to find the lcm of two numbers. It is a number python program.

To understand this example, you should have knowledge of the following Python programming topics:

In this number python program, we need to find the **LCM**. In Arithmetic theory, the **LCM **is called the **least common multiple**. which means the smallest integer which is a multiple of both **a** and **b** It can also be described as the smallest number, which is divisible by both numbers. Lcm of two numbers is denoted by **lcm( a, b)**. Let us take an example of **lcm(5, 2)** so the multiples of 5 are 5, 10, 15, 20, 25 .... and the multiples of 2 are 2, 4, 6, 8, 10, 12... In this case, when we take the LCM, we will get the answer as 10.

In this simple python program, we need to apply this logic to find the** LCM **of given numbers. So we have first to identify the greater number is using an if condition in python language, which means in the previous example, the greater number is **5**. Now we apply the logic as we start from the greater value. and we apply the **mod **operator with that greater value and **x, y**. We call a user-defined function in python to do the **Lcm **calculation.

If both conditions satisfy, then we print lcm as greater. or we increment greater with** 1** and continue that python while loop till we get a value that is divisible by both **x** and **y**.

Let us take the previous example in this beginner-level python program. We start with** **5 as greater and we do the **mod** operation with **x** and **y** and it will be 5 **mod **5 and** **5 **mod **2 it will be satisfied in first but not in second with 2. So we increment greater with 1 and it will be 6. then 6 **mod **5 and 6 **mod **2 again and again, until greater will be 10. Then it will be satisfied, and we print **LCM as 10**.

**STEP 1:** Accept the input from the user using the input function in the python programming language. And save the value in two variables using** int()**.

**STEP 2:** Call the function lcm with **num1 **and **num2** as parameters. Pass these parameters to the function and print the return result as LCM using print in python language.

**STEP 1:** Check If the **x** is greater than **Y**.

**STEP 2:** Assign the **greater = x**.

**STEP 3:** Use else to assign **greater = y** if the condition is False.

**STEP 4:** Starting the loop using a while.

**STEP 5:** Checking the condition greater **mod x **and greater **mod y** are true.

**STEP 6:** If that is true, lcm is greater and breaks the loop.

**STEP 7:** Else increment greater by **1** and return the result to the function.

` ````
def lcm(x, y):
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number:"))
print("The L.C.M. is", lcm(num1, num2))
```

Enter first number: 3 Enter first number: 6 The L.C.M. is 6