About the program
This Python program reads two integers from the user and computes their greatest common divisor (GCD) with recursive logic.
It uses the recursive Euclidean algorithm, one of the fastest and simplest ways to compute the GCD.
Python code:
def gcd(a: int, b: int) -> int:
if b == 0:
return a
else:
return gcd(b, a % b)
# Run the program
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
result = gcd(num1, num2)
print(f"The greatest common divisor of {num1} and {num2} is: {result}")Sample output (input: 48 and 18):
The greatest common divisor of 48 and 18 is: 6How it works:
- Two integers are read from the user
- The gcd function runs the Euclidean algorithm recursively
- At each step the function calls itself with (b, a % b) until b becomes zero
- The value of a at that point is the GCD