Python GCD code using math.gcd() Function

By | July 26, 2019

Python GCD code using math.gcd() function – This Python programming tutorial explains the method of finding GCD of two numbers using math module’s gcd() function.

What is Python gcd() functin of math module

The math.gcd(a,b) function return the greatest common divisor of the integers a and b. Therefore, gcd(a,b) function will calculate the largest positive integer that divides both a and b. Note that gcd(0, 0) returns 0.

Python GCD code using math.gcd(a,b) Function

# Write a Python program to find
# GCD of two numbers using gcd(a,b)
# function of math module
 
# Author: www.EasyCodeBook.com (c)

import math
num1= int(input('Enter the first number='))
num2= int(input('Enter the second number='))

result = math.gcd(num1,num2)

print('GCD of',num1,'and',num2,'is:',result)

The output of the program using gcd() function

Enter the first number=60
Enter the second number=80
GCD of 60 and 80 is: 20

Another sample run of the program:
Enter the first number=21
Enter the second number=101
GCD of 21 and 101 is: 1
Python GCD code using math.gcd() Function

Python GCD code using math.gcd() Function

Leave a Reply

Your email address will not be published. Required fields are marked *