Google News
logo
Python Program to Find the GCD of Two Numbers
Here's a Python program to find the GCD (Greatest Common Divisor) of two numbers :
Program :
def find_gcd(num1, num2):
    # Find the smaller number between num1 and num2
    if num1 < num2:
        smaller = num1
    else:
        smaller = num2
    
    # Loop through possible divisors of the smaller number
    for i in range(1, smaller+1):
        if num1 % i == 0 and num2 % i == 0:
            gcd = i
    
    return gcd

# 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 works by finding the smaller number between the two, and then looping through possible divisors of the smaller number until it finds the greatest common divisor that is also a divisor of the other number.

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.