gcd() function Python

PythonServer Side ProgrammingProgramming

Greatest common divisor or gcd is a mathematical expression to find the highest number which can divide both the numbers whose gcd has to be found with the resulting remainder as zero. It has many mathematical applications. Python has a inbuilt gcd function in the math module which can be used for this purpose.


It accepts two integers as parameter and returns the integer which is the gcd value.


Syntax: gcd(x,y)
Where x and y are positive integers.

Example of gcd()

In the below example we print the result of gcd of a pair of integers.

import math
print ("GCD of 75 and 30 is ",math.gcd(75, 30))
print ("GCD of 0 and 12 is ",math.gcd(0, 12))
print ("GCD of 0 and 0 is ",math.gcd(0, 0))
print ("GCD of -24 and -18 is ",math.gcd(-24, -18))


Running the above code gives us the following result −

GCD of 75 and 30 is 15
GCD of 0 and 12 is 12
GCD of 0 and 0 is 0
GCD of -24 and -18 is 6
Updated on 08-Aug-2019 06:53:53