- Related Questions & Answers
- Bitset all() function in C++ STL
- Write a function that returns 2 for input 1 and returns 1 for 2 in C programming
- 10 Interesting Python Cool Tricks
- howdoi in Python
- How to print without newline in Python?
- How to download Google Images using Python
- How to clear screen in python?
- get() method for dictionaries in Python
- Generating random number list in Python
- frozenset() in Python
- floor() and ceil() function Python
- abs() function for complex number in c++ ?
- float() in Python
- Finding Mean, Median, Mode in Python without Libraries
- Find the k most frequent words from data set in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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

Advertisements