Python Program to Find LCM of Two Numbers using For loop
ADVERTISEMENTS
Python program to find LCM of two numbers using for loop. In this article, you will learn how to find lcm of two numbers in python language using for loop.
Python Program to Find LCM of Two Numbers using For loop
# Python Program to Find LCM of Two Numbers using For loop
p, q, x, g = 0, 0, 0, 0
# p & q - To store the two positive numbers
# x - To store the LCM number
# g - To store the GCD
print ("-----Enter the two positive integer numbers-----")
p = int (input ())
q = int (input ())
for i in range (1, p + 1):
if i <= q:
if p % i == 0 and q % i == 0:
g = i
x = (p * q) / g
print ("\nThe LCM of two positive numbers ", p, " & ", q, " is ", x, ".")
Output
-----Enter the two positive integer numbers-----
120
140
The LCM of the 120 and 140 numbers is 840.
Python Program to Find LCM of Two Numbers using While loop
# Python Program to Find LCM of Two Numbers using While loop
p, q, x = None, None, None
# p & q - To store the two positive numbers
# x - To store the LCM number
print ("-----Enter the two positive integer numbers-----")
p = int (input ())
q = int (input ())
x = p if p > q else q
while True:
if x % p == 0 and x % q == 0:
print ("\nThe LCM of the ", p, " and ", q, " numbers is ", x, ".")
break
x += 1
Output
-----Enter the two positive integer numbers-----
130
170
The LCM of two positive numbers 130 & 170 is 2210.0.