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

Data Mining Techniques & Tools: Types of Data, Methods, Applications [With Examples]
101506
Why data mining techniques are important like never before? Businesses these days are collecting data at a very striking rate. The sources of this eno
Read More

by Rohit Sharma

07 Jul 2024

An Overview of Association Rule Mining & its Applications
142267
Association Rule Mining in data mining, as the name suggests, involves discovering relationships between seemingly independent relational databases or
Read More

by Abhinav Rai

07 Jul 2024

What is Decision Tree in Data Mining? Types, Real World Examples & Applications
16859
Introduction to Data Mining In its raw form, data requires efficient processing to transform into valuable information. Predicting outcomes hinges on
Read More

by Rohit Sharma

04 Jul 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
82628
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

04 Jul 2024

Most Common Binary Tree Interview Questions & Answers [For Freshers & Experienced]
10093
Introduction Data structures are one of the most fundamental concepts in object-oriented programming. To explain it simply, a data structure is a par
Read More

by Rohit Sharma

03 Jul 2024

Data Science Vs Data Analytics: Difference Between Data Science and Data Analytics
70155
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

02 Jul 2024

Graphs in Data Structure: Types, Storing & Traversal
51863
In my experience with Data Science, I’ve found that choosing the right data structure is crucial for organizing information effectively. Graphs
Read More

by Rohit Sharma

01 Jul 2024

Python Banking Project [With Source Code] in 2024
14952
The banking sector has many applications for programming and IT solutions. If you’re interested in working on a project for the banking sector,
Read More

by Rohit Sharma

25 Jun 2024

Linear Search vs Binary Search: Difference Between Linear Search & Binary Search
66270
In my journey through data structures, I’ve navigated the nuances of linear search vs binary search in data structure, especially when dealing w
Read More

by Rohit Sharma

23 Jun 2024

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