Python Program to find GCD

You are Here:

What is Greatest Common Divisor?

A largest number that exactly divides two or more integers.

In general, Greatest Common Divisor (GCD) is otherwise called as Greatest Common Factor (GCF) or Highest Common Factor (HCF)

Tips: It is recommended to use our online GCD calculator for better understanding.

Examples

The following table provides few examples of GCD of the given numbers.

NumbersGCD
4, 102
6, 51
15, 30 , 4515

GCD of Two Numbers

In the following example, we will find the GCD of the given two numbers (45, 90).

Example

Python Compiler
num1 = 45 num2 = 90 gcd = 1 min = (num1 < num2) and num1 or num2; for i in range(1, min+1): if((num1 % i == 0) and (num2 % i == 0)): gcd = i print("GCD of %d and %d: %d" % (num1, num2, gcd))

Output

GCD of 45 and 90: 45

GCD of any Two Given Numbers

In the following example, we will find the GCD of any two given numbers.

Example

Python Compiler
num1 = int(input("Enter (int) num1 = ")) num2 = int(input("Enter (int) num1 = ")) gcd = 1 min = (num1 < num2) and num1 or num2; for i in range(1, min+1): if((num1 % i == 0) and (num2 % i == 0)): gcd = i print("\nGCD of %d and %d: %d" % (num1, num2, gcd))

Output

Enter (int) num1 = 14 Enter (int) num1 = 25 GCD of 14 and 25: 1

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.

We are working to cover every Single Concept in Python.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author