- Related Questions & Answers
- How to Find HCF or GCD using Python?
- Find original numbers from gcd() every pair in Python
- Grand Central Dispatch (GCD)
- Minimum removals from array to make GCD Greater in Python
- Find GCD of two numbers
- C++ Program to Find GCD
- Python Program for GCD of more than two (or array) numbers
- GCD of more than two (or array) numbers in Python Program
- Euclidean Algorithm for calculating GCD in JavaScript
- Finding gcd of two strings in JavaScript
- Python zip() Function
- Intersection() function Python
- Python - fmod() function
- Python - int() function
- Python frexp() Function

- 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