Python Program to Find GCD (Greatest Common Divisor) of Two Numbers

The Greatest Common Divisor (GCD), also known as the Highest Common Factor (HCF), is one of the most important concepts in mathematics and programming. It is used in simplifying fractions, cryptography, and various algorithmic problems. In this tutorial, we will learn how to calculate the GCD of two numbers in Python using different approaches, including basic methods and the efficient Euclidean algorithm.

What is GCD?

The GCD of two integers is the largest number that divides both of them without leaving a remainder. For example: - GCD of 12 and 18 is 6 - GCD of 20 and 30 is 10 It is widely used in mathematics and computer science.

Why Learn GCD Program?

Learning how to compute the GCD helps you understand loops, conditional logic, and optimization techniques. It is also commonly used in competitive programming and real-world applications such as encryption and data processing.

Step-by-Step Algorithm (Basic Method)

  • Start the program
  • Input two numbers
  • Find the smaller number
  • Loop from 1 to the smaller number
  • Check if both numbers are divisible
  • Store the greatest divisor
  • Display the result
  • End the program

Python Program (Basic Method)

Python
# GCD using basic method

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

small = min(num1, num2)
gcd = 1

for i in range(1, small + 1):
    if num1 % i == 0 and num2 % i == 0:
        gcd = i

print("GCD:", gcd)
Sample Output:

Enter first number: 12
Enter second number: 18
GCD: 6

Code Explanation

In this method, we iterate from 1 to the smaller of the two numbers and check which numbers divide both inputs. The largest such number is the GCD.

Euclidean Algorithm (Efficient Method)

Python
# GCD using Euclidean algorithm

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

while num2 != 0:
    num1, num2 = num2, num1 % num2

print("GCD:", num1)

Why Euclidean Algorithm?

The Euclidean algorithm is much faster than the basic method, especially for large numbers. It reduces the problem size at each step, making it highly efficient.

Using Function

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

x = int(input("Enter first number: "))
y = int(input("Enter second number: "))

print("GCD:", gcd(x, y))

Using Built-in Function

Python
import math

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("GCD:", math.gcd(num1, num2))

Handling Negative Numbers

Python
import math

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("GCD:", math.gcd(abs(num1), abs(num2)))

Real-World Applications

  • Simplifying fractions
  • Cryptography algorithms
  • Number theory problems
  • Data processing and optimization
  • Computer graphics calculations

Common Mistakes to Avoid

  • Using inefficient loops for large numbers
  • Incorrect modulo operation
  • Not handling zero values
  • Ignoring negative numbers
  • Incorrect initialization

Advanced Enhancements

  • Find LCM using GCD
  • Extend to multiple numbers
  • Build a math calculator
  • Use in encryption algorithms
  • Optimize for large datasets

Practice Exercises

  • Find LCM of two numbers
  • Calculate GCD for multiple inputs
  • Build a menu-driven calculator
  • Compare performance of methods
  • Create number theory tool

Conclusion

The GCD program is an essential concept that helps you understand loops, conditions, and efficient algorithms. By learning both basic and optimized methods, you can solve problems more effectively and apply these concepts in real-world scenarios.

Note: Note: Always prefer the Euclidean algorithm for better performance with large numbers.