How to use the math.gcd() function in Python

The Math Module has many functions in Python for mathematical operations. math.gcd() is a function that gives us the greatest common divisor of two numbers when given.

Syntax

math.gcd(x,y)

Parameter

This function accepts two parameters, x and y, to calculate the greatest common divisor of two parameters.

Output

This function outputs the greatest common divisor of two numbers that are passed as parameters to the function.

Static code

import math
print(math.gcd(12,15))

Dynamic code

In dynamic code, we take the values of the two parameters, using int(input()), so that we can pass the input as integers to the gcd function.

Make sure you add the two numbers in the input column below before pressing the run button.

import math
a=int(input("Enter your first number:"))
b=int(input("Enter your second number:"))
print("The greatest common divisor of the two numbers is:",math.gcd(a,b))

Enter the input below

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources