Compute the greatest common divisor and least common multiple of two integers

 

# GCD PROGRAM

num1 = int(input("Enter 1st number: "))
num2 = int(input("Enter 2nd number: "))
i = 1
while(i <= num1 and i <= num2):
  if(num1 % i == 0 and num2 % i == 0):
    gcd = i
  i = i + 1
print("Greatest Common Divisor (GCD) is ", gcd)

# LCM PROGRAM

if num1 > num2:
    greater = num1
else:
    greater = num2

while(True):
    if((greater % num1 == 0) and (greater % num2 == 0)):
        lcm = greater
        break
    greater += 1

print("The Least Common Multiple (LCM) is ", lcm)

 

Output:

Enter 1st number: 3
Enter 2nd number: 5
('Greatest Common Divisor (GCD) is ', 1)
('The Least Common Multiple (LCM) is ', 15)
>>>

 

Enter 1st number: 5
Enter 2nd number: 10
('Greatest Common Divisor (GCD) is ', 5)
('The Least Common Multiple (LCM) is ', 10)
>>>

 

 

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved
error: Content is protected !!