Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconPython Program to Find Two GCD Numbers

Python Program to Find Two GCD Numbers

Last updated:
13th Jun, 2023
Views
Read Time
4 Mins
share image icon
In this article
Chevron in toc
View All
Python Program to Find Two GCD Numbers

The Greatest Common Divisor in Mathematics is abbreviated to GCD. This number represents the largest value that has the ability to divide the two given numbers completely. It is also referred to as the greatest common factor or GCF in few cases. Let us consider an example of two numbers, 12 and 28. The numbers that can divide both of these numbers are 2 and 4. However, ‘4’ is the greatest number that can divide both 12 and 24 completely. Hence, the GCD of 12 and 24 is 4. The GCD can be found for any number of inputs. It is just the common largest number that can divide all the input numbers. Finding GCD of a number finds a wide range of applications in encryption technology. It is also used in the simplification of fractions. 

Check out our other data science courses at upGrad. 

gcd() in Python:

A built-in function exists in the Python interpreter’s math module that can compute the GCD of any two input numbers. This inbuilt function is the GCD() function. The gcd() function in Python accepts two parameters of integer type and returns an integer that is equivalent to the GCD value of the two input parameters. 

The general syntax of the gcd() function in Python is:

Gcd(x,y)

The mathematical phrase for determining the biggest common factor that may precisely divide two numbers is “greatest common divisor” (GCD). The Highest Common Factor (HCF) is another name for a GCD. The HCF/ GCD, for instance, of the two numbers 48 and 24 is 8 because 8 entirely divides 48 and 24.

Explore our Popular Data Science Courses

Parameters:

The gcd() function accepts two parameters to compute the gcd. Both the parameters must be positive integers.

Read our popular Data Science Articles

Return value of gcd in Python:

The gcd() function in Python returns a positive integer whose value is equivalent to the greatest common divisor of the input parameters. 

Apart from using the gcd() function, there are several other methods and approaches to determine the GCD of two numbers in Python. The approaches include:

  • Determining GCD using recursions
  • Finding GCD using loops
  • Implementation of GCD using Euclidean Algorithm

You can learn both theoretical and practical aspects of Python with upGrad’s Professional Certificate in Data Science and Business Analytics from the University of Maryland. This course helps you learn Python from scratch. Even if you are new to programming and coding, upGrad will offer you a two-week preparatory course so that you can pick up on the basics of programming. you will learn about various tools like Python, SQL,, while working on multiple industry projects.

Techniques To Find GCD Of Two Numbers In Python 

1. Using gcd()

The first method to find the GCD of two numbers in Python uses an in-built function called gcd(). The syntax for the same is ‘gcd(a,b)’, where a and b  are the two integer numbers passed as an argument to the function. Below is a GCD program in Python to achieve the same:

import math

print (math.gcd(3, 6))

Output:

3

Note: To use the gcd() function to evaluate the GCD of two numbers in Python, importing the math module is required. ImportError will be thrown if the math module is not imported.

2. Using Loops

The second method to find GCD in Python is using the loops. In computer programming, a loop is a collection of instructions repeatedly carried out until a particular condition is satisfied. Below mentioned is the Python program to do the same:

def calculate_gcd(a,b):

if a>b:

var = b

else:

var = a

for j in range(1, var + 1):

if((a % j == 0) and (b % j == 0)):

req_gcd = j

return req_gcd

x=24

y=36

print(calculate_gcd(x,y))

Output:

12

3. Using Recursion

Recursion is the third way to find the GCD. It is the act of repeating things in a self-similar war. Recursive calling of the function is used in programming languages to describe when a program allows calling a function inside another function. The Python code to achieve the same is listed below:

def cal_gcd(x,y):

if(y == 0):

return abs(x)

else:

return cal_gcd(y, x % y)

a=24

b=36

print(cal_gcd(a,b))

Output:

12

4. Euclidean Algorithm

Using the Euclidean Algorithm is also a unique way to find GCD. The foundation of this technique is the fact that the HCF of any two numbers also divides the difference of the two numbers. In this procedure, the greater is divided by, the smaller, and the residual is taken. Divide the smaller by the leftover now. Continue until there is no more to add.

The Python program to accomplish the same task is provided below:

def cal_gcd(a, b):

while(b):

a,b = b,a % b

return abs(a)

a=24

b=36

print(cal_gcd(a,b))

Output:

12

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Explore Free Courses

Suggested Blogs

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
77875
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
135165
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Data Science Vs Data Analytics: Difference Between Data Science and Data Analytics
66664
Summary: In this article, you will learn, Difference between Data Science and Data Analytics Job roles Skills Career perspectives Which one is right
Read More

by Rohit Sharma

19 Feb 2024

13 Exciting Python Projects on Github You Should Try Today [2023]
44179
Python is one of the top choices in programming languages among professionals worldwide. Its straightforward syntax allows software developers and dat
Read More

by Hemant

19 Feb 2024

Top 15 Python AI & Machine Learning Open Source Projects
35798
Machine learning and artificial intelligence are some of the most advanced topics to learn. So you must employ the best learning methods to make sure
Read More

by Pavan Vadapalli

19 Feb 2024

Top 21 Python Developer Skills You Must Need To Become a Successful Python Developer
77920
Its intuitive syntax, extensive libraries and versatile integration capabilities have fueled incredible growth across web development and scientific c
Read More

by Rohit Sharma

19 Feb 2024

Most Frequently Asked NumPy Interview Questions and Answers [For Freshers]
28427
If you are looking to have a glorious career in the technological sphere, you already know that a qualification in NumPy is one of the most sought-aft
Read More

by Rohit Sharma

19 Feb 2024

Top 30 Python Pattern Programs You Must Know About
32572
Summary Pattern in Python or “Python patterns” is an essential part of Python programming, especially when you are just starting out with using algor
Read More

by Rohit Sharma

19 Feb 2024

Top 12 Fascinating Python Applications in Real-World [2024]
154298
It is a well-established fact that Python is one of the most popular programming languages in both the coding and Data Science communities. But have y
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon