Example HTML page

A Python program to find LCM(using functions)

In the earlier post we have seen how to find LCM using loops. In this post we will see how to find LCM using functions.

#Program

def lcm(p,q):
    if p>q:
        high=p
    else:
        high=q
    while(True):
        if((high % p==0) and (high % q==0)):
            lcm=high
            break
        high+=1
    return lcm
n1=int(input("enter value of p:"))
n2=int(input("enter value of q:"))
print("LCM of",n1,"and",n2,"is",lcm(n1,n2))

Output

enter value of p: 12

enter value of q: 24

LCM of 12 and 24 is 24

 Explanation:

In the above program we are defining a function definition as “def lcm” and passed parameters p and q. Now initiating a loop, in that loop we are finding the highest number which is divisible by p and q 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 function definition is called and you will be displayed the output.

 

Example HTML page

Leave a Reply

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

Pin It on Pinterest