Python Program to Find the Gcd of Two Numbers Last Updated : 22 Feb, 2025 Comments Improve Suggest changes Like Article Like Report The task of finding the GCD (Greatest Common Divisor) of two numbers in Python involves determining the largest number that divides both input values without leaving a remainder. For example, if a = 60 and b = 48, the GCD is 12, as 12 is the largest number that divides both 60 and 48 evenly. Using euclidean algorithmEuclidean algorithm repeatedly replaces the larger number with the remainder of the division until the remainder is zero. The last non-zero divisor is the GCD. Python a = 60 # first number b = 48 # second number # loop until the remainder is 0 while b != 0: a, b = b, a % b print(a) Output12 Explanation: while loop runs until b becomes 0. In each iteration, a is updated to b and b is updated to a % b. When b becomes 0, the value of a is the GCD .Table of ContentUsing math.gcd()Using subtraction based gcdUsing math.gcd()math.gcd() function is a built-in function in python hence an efficient way to find the GCD of two numbers in Python, internally using the Euclidean algorithm. Python import math a = 60 # first number b = 48 # second number print(math.gcd(a, b)) Output12 Explanation: math.gcd(a, b) takes a and b as arguments and returns their GCD. when it is called, it computes the GCD and directly returns the result.Using subtraction based gcdThis method repeatedly subtracts the smaller number from the larger one until both numbers become equal, resulting in the GCD. Python a = 60 # first number b = 48 # second number while a != b: if a > b: a -= b # subtract b from a if a is greater else: b -= a # subtract a from b if b is greater print(a) Output12 Explanation: while loop runs until a becomes equal to b. In each iteration, if a is greater than b, b is subtracted from a otherwise, a is subtracted from b. When both values become equal, that value is the GCD. Comment More infoAdvertise with us Next Article Python Program to Find the Gcd of Two Numbers B biswasarkadip Follow Improve Article Tags : Python Python Programs GCD-LCM python Practice Tags : pythonpython Similar Reads Python Program to Find LCM of Two Numbers We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python.Example:Input: a = 12, b = 15Output: 60Explanation: LCM of 12 and 15 is 60Python Program to Find LCM of Two NumbersBelow 3 min read Python program to Find the Jumbo GCD subarray Given an array, write a program to find the maximum GCD among all the subarray of the size >= 2 of the given array. Examples: Input list: [2, 3, 4, 4, 4] Output: 4 Input list: [3, 7, 2, 9, 18, 5, 1, 13 ] Output: 9Approach:Import the math module for pythonIntroduce a variable(say, V1) to store the 2 min read Python Program to find the Quotient and Remainder of two numbers Given two numbers n and m. The task is to find the quotient and remainder of two numbers by dividing n by m. Examples: Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 4 Method 1: Naive approach The naive approach is to find the quotient using the 2 min read Python Program to Find Numbers Divisible by Another Number We are given a list of numbers and a number. We have to find all the numbers in the list that are divisible by the given single number. Examples:Input: list=[8, 14, 21, 36, 43], num=3Output: 21, 36, 57Input: list=[2, 17, 25, 31, 48, 55], num=5Output: 25, 55In this article, we will discuss the differ 3 min read Program to Find GCD or HCF of Two Numbers Given two positive integers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4 12 min read Like