Python Program to find GCD of Two Numbers

Write a Python program to find GCD of two numbers using While Loop, Functions, and Recursion. To find the GCD or HCF in Python, we have to pass at least one non-zero value

The Greatest Common Divisor is also known as Highest Common Factor (HCF), or Greatest Common Factor (GCF), or Highest Common Divisor (HCD), or Greatest Common Measure (GCM).

In Mathematics, the Greatest Common Divisor of two or more integers is the largest positive integer that divides given integer values without the remainder. For example, the GCD value of integer 8 and 12 is 4 because both 8 and 12 are divisible by 1, 2, and 4 (the remainder is 0), and the largest positive integer among them is 4.

Python Program to find GCD of Two Numbers Example 1

This python program allows the user to enter two positive integer values. Next, we are using the Python While loop to restrict the i value not to exceed the user specified values.

Within While loop, we used the If Statement to check whether a%i and a % i remainder equals to zero or not. If true, Highest Common Factor = I otherwise, skip that value.

a = float(input(" Please Enter the First Value a: "))
b = float(input(" Please Enter the Second Value b: "))

i = 1
while(i <= a and i <= b):
    if(a % i == 0 and b % i == 0):
        val = i
    i = i + 1
    
print("\n HCF of {0} and {1} = {2}".format(a, b, val))
 Please Enter the First Value a: 8
 Please Enter the Second Value b: 12

 HCF of 8.0 and 12.0 = 4

Python Program to find HCF of Two Numbers Example 2

It is another approach to find the Greatest Common Factor of two numbers. In this program, we are using the Temp variable to find GCD.

num1 = float(input(" Please Enter the First : "))
num2 = float(input(" Please Enter the Second : "))

a = num1
b = num2

while(num2 != 0):
    temp = num2
    num2 = num1 % num2
    num1 = temp

hcf = num1   
print("\n HCF of {0} and {1} = {2}".format(a, b, hcf))
 Please Enter the First : 12
 Please Enter the Second : 36

 HCF of 12.0 and 36.0 = 12.0

Python Program to find GCD of Two Numbers without using Temp

In this program, we are finding the GCD without using the Temp variable.

num1 = float(input(" Please Enter the First : "))
num2 = float(input(" Please Enter the Second : "))

a = num1
b = num2

if(num1 == 0):
    print("\n HCF of {0} and {1} = {2}".format(a, b, b))

while(num2 != 0):
    if(num1 > num2):
        num1 = num1 - num2
    else:
        num2 = num2 - num1

hcf = num1   
print("\n HCF of {0} and {1} = {2}".format(a, b, hcf))
 Please Enter the First : 75
 Please Enter the Second : 255

 HCF of 75.0 and 255.0 = 15.0

Python Program to find GCD of Two Numbers using Functions

This Python program is the same as above. However, we are separating the logic using Functions

def findresult(val1, val2):
    if(val1 == 0):
        print("\n HCF of {0} and {1} = {2}".format(a, b, b))

    while(val2 != 0):
        if(val1 > val2):
            val1 = val1 - val2
        else:
            val2 = val2 - val1
    return val1

a = float(input(" Please Enter the First Value a: "))
b = float(input(" Please Enter the Second Value b: "))

result = findresult(a, b)  
print("\n HCF of {0} and {1} = {2}".format(a, b, result))
Python Program to find GCD of Two Numbers 4

Python Program to Calculate GCD of Two Numbers using Recursion

It allows user to enter two positive integer values, and calculate the Greatest Common Divisor of those two values by calling findGreatestCD function recursively.

def findGreatestCD(a, b):
    if(b == 0):
        return a;
    else:
        return findGreatestCD(b, a % b)
    
num1 = float(input(" Please Enter the First Value  Num1 : "))
num2 = float(input(" Please Enter the Second Value Num2 : "))

Val = findGreatestCD(num1, num2)
print("\n The Result of {0} and {1} = {2}".format(num1, num2, Val))
 Please Enter the First Value  Num1 : 22
 Please Enter the Second Value Num2 : 88

 The Result of 22.0 and 88.0 = 22.0