CBSE 11TH CLASS CS PROGRAM 9

 

9. Compute the greatest common divisor and least common multiple of two integers. 1  

 

Python

def gcd(a, b):

    while b:

        a, b = b, a % b

    return a

 

def lcm(a, b):

    return (a * b) // gcd(a, b)

 

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

 

print("GCD of", num1, "and", num2, "is", gcd(num1, num2))

print("LCM of", num1, "and", num2, "is", lcm(num1, num2))

No comments:

Post a Comment