Google News
logo
Python Program to Find the GCD of Two Numbers using Recursion
Here's a Python program to find the GCD (Greatest Common Divisor) of two numbers using recursion :
Program :
def find_gcd(num1, num2):
    if num2 == 0:
        return num1
    else:
        return find_gcd(num2, num1 % num2)

# Example usage
print("GCD of 12 and 18 is", find_gcd(12, 18))
Output :
GCD of 12 and 18 is 6
In this program, the `find_gcd()` function takes two numbers as input parameters and returns their GCD.

The function uses recursion to calculate the GCD. If `num2` is equal to zero, then `num1` is the GCD of the two numbers. Otherwise, the function calls itself with `num2` as the first parameter and the remainder of `num1` divided by `num2` as the second parameter.

The program prints the GCD of 12 and 18 as an example. You can replace these numbers with any two integers to find their GCD.