πŸ”₯ LIVE πŸ‘©β€πŸ« Teachers: Host Live Tests  |  πŸŽ“ Students: Practice Class-wise Quizzes Go to Quiz Portal βž”

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

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)
>>>

 

 

Jitendra Singh
βœ” Verified Educator

Jitendra Singh

Founder of CBSEPython.in

I help CBSE Class 9–12 students learn Python, Information Technology, Artificial Intelligence and Computer Science through easy notes, quizzes, MCQs and sample papers.

Read More About Me β†’

Leave a Comment