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.
RUN CODE SNIPPET# 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 else: greater = y while(True): if((greater % x == 0) and (greater % y == 0)): lcm = greater break 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))
INPUT:
54 24
OUTPUT:
Enter the first integer: Enter the second integer: The L.C.M. is 216
- At the start, we use
def compute_lcm(x, y)
followed by a:
where thedef
keyword is used to define a function and thecompute_lcm(x, y)
function returns the L.C.M of two numbers. - After that we declare an
if
statement followed by a:
with conditionx > y
where if the condition is satisfied, x is considered greater. If not, it moves to theelse
statement, where y is considered greater. - Next, we declare a
while
loop , with anif
condition((greater % x == 0) and (greater % y == 0))
wherelcm = greater
followed by a:
. - We give a
break
after that, wheregreater += 1
. Once the loop iterates till the conditions are met and comes to an end, thereturn
function returns the value oflcm
. - 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 variablenum1
andnum2
with the statements/strings("Enter the first integer: ")
and("\nEnter the second integer: ")
, we use theint
function and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- The final output values are displayed with the statement
("\nThe L.C.M. is", compute_lcm(num1, num2))
using theprint
function, which displays the computed values of the integers stored in the variablesnum1
andnum2
.
NOTE:
- 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.