Question:
Program to find the LCM of two numbers
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# Python Program to find the L.C.M. of two input number # defining a user defined function for finding LCM def lcm(x, y): # choose the greater number if x > y: large = x else: large = y while(True): if((large % x == 0) and (large % y == 0)): lcm = large break large += 1 return lcm nos1 = int(input("Enter first number: ")) nos2 = int(input("Enter second number: ")) print "The L.C.M. of", nos1,"and", nos2,"is", lcm(nos1, nos2) |
Explanation:
A common multiple is a number that is a multiple of two or more numbers. The common multiples of 3 and 4 are 0, 12, 24, …. The least common multiple(LCM) of two numbers is the smallest number (not zero) that is a multiple of both.
Output:
1 2 3 4 5 |
Enter first number: 6 Enter second number: 3 The L.C.M. of 6 and 3 is 6 |
Leave a Reply