Example HTML page

A Python program to find LCM(using loops).

In this post we will see how to find LCM between the numbers using loops.

To calculate LCM(using loops):

LCM which is abbreviated as “Least Common Multiple/Lowest Common Multiple”. LCM of x and y are denoted as LCM(x,y). In short, it is said as the smallest positive integer which is divisible by both “x” and “y”.

Ex: LCM(6,8).

6 multiples are : 6,12,18,24,30,36,42,48 and so on

8 multiples are : 8,16,24,32,40,48,56,64,72 and so on.

Here the common  multiples of 6 and 8 are 24,48 and so on.But the least common multiple from both is 24 and hence the LCM(6,8) is 24.

n1=6
n2=8
if n1 > n2:  
   high = n1  
else:  
   high = n2  
while(True):  
   if((high % n1 == 0) and (high % n2 == 0)):  
        lcm = high  
        break  
   high =  high + 1
print ("LCM of",n1,"and",n2,"is","=", lcm)

Output

LCM of 6 and 8 is = 24

Explanation:

We are finding the number which is divisible by both the numbers known as LCM. In the above program, initially we are evaluating highest number between the two given numbers and initiate a loop. In that loop we are finding the number which is divisible by n1 and n2 as well and this number is stored in a variable called “LCM”. If it is not possible to find that number, we incremented that  number by “1”. Finally there prints the output of LCM.

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest