Python Program to Find LCM of two Numbers

In this python tutorial, you will learn how to Find LCM of two Numbers using the if and else statements, while loop along with the different operators of the python programming language.

How to to Find LCM of two Numbers?

Let’s take a look at the source code , here the values are given as input by the user in the code, the operators and while loop along with the if and else statements carry out the function.

# Python Program to find the L.C.M. of two input number

def compute_lcm(x, y):

   # choose the greater number
   if x > y:
       greater = x
       greater = y

       if((greater % x == 0) and (greater % y == 0)):
           lcm = greater
       greater += 1

   return lcm

num1 =int(input("Enter the first integer: "))
num2 =int(input("\nEnter the second integer: ")) 

print("\nThe L.C.M. is", compute_lcm(num1, num2))




Enter the first integer: 
Enter the second integer: 
The L.C.M. is 216
  1. At the start, we use def compute_lcm(x, y) followed by a : where the def keyword is used to define a function and the compute_lcm(x, y) function returns the L.C.M of two numbers.
  2. After that we declare an if statement followed by a : with condition x > y where if the condition is satisfied, x is considered greater. If not, it moves to the else statement, where y is considered greater.
  3. Next, we declare a while loop , with an if condition ((greater % x == 0) and (greater % y == 0)) where lcm = greater followed by a :.
  4. We give a break after that, where  greater += 1. Once the loop iterates till the conditions are met and comes to an end, the return function returns the value of lcm .
  5. Here we give the user the option to enter the values and the input values are scanned using the input function which are stored in the variable  num1 and num2 with the statements/strings ("Enter the first integer: ") and ("\nEnter the second integer: "), we use the int function and declare the  input value as an integer value.
  6. In the STDIN section of the code editor the input values are entered.
  7. The final output values are displayed with the statement ("\nThe L.C.M. is", compute_lcm(num1, num2))using the print function, which displays the computed values of the integers stored in the variables num1 and num2.


  • The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
  • The While true loop in python runs without any conditions until the break statement executes inside the loop.
  • The Break statement in python stops the loop in which the statement is placed.
  • The Addition Assignment+= operator in python adds two values together and assign the resultant value to a variable.
  •  The Comparison operator == determines if the values of two objects are equal.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement/string to be displayed in enclosed in double quotes.
  • The input() function allows a user to insert a value into a program, it returns a integer value.
  • The statement for the input function are enclosed in single quotes and parenthesis.