CBSE Class 12 Computer Science Question 59 of 101

Functions — Question 27

Back to all questions
27
Question

Question 23

Write a program to find the greatest common divisor between two numbers.

Solution
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
gcd_value = gcd(num1, num2)
print("The greatest common divisor of", num1, "and", num2, "is", gcd_value)
Output
Enter the first number: 24
Enter the second number: 6
The greatest common divisor of 24 and 6 is 6
Answer