Gcd using recursion in python

This is a Python Program to find the GCD of two numbers using recursion.

Problem Description

The program takes two numbers and finds the GCD of two numbers using recursion.

Problem Solution

1. Take two numbers from the user.
2. Pass the two numbers as arguments to a recursive function.
3. When the second number becomes 0, return the first number.
4. Else recursively call the function with the arguments as the second number and the remainder when the first number is divided by the second number.
5. Return the first number which is the GCD of the two numbers.
6. Print the GCD.
7. Exit.

Program/Source Code

Here is source code of the Python Program to find the GCD of two numbers using recursion. The program output is also shown below.

def gcd(a,b):
    if(b==0):
        return a
    else:
        return gcd(b,a%b)
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
GCD=gcd(a,b)
print("GCD is: ")
print(GCD)

Program Explanation

1. User must enter two numbers.
2. The two numbers are passed as arguments to a recursive function.
3. When the second number becomes 0, the first number is returned.
4. Else the function is recursively called with the arguments as the second number and the remainder when the first number is divided by the second number.
5. The first number is then returned which is the GCD of the two numbers.
6. The GCD is then printed.

Runtime Test Cases

 
Case 1:
Enter first number:5
Enter second number:15
GCD is: 
5
 
Case 2:
Enter first number:30
Enter second number:12
GCD is: 
6

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Gcd using recursion in python

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.


Suppose we have two numbers a and b. We have to find the GCD of these two numbers in recursive way. To get the GCD we shall use the Euclidean algorithm.

So, if the input is like a = 25 b = 45, then the output will be 5

To solve this, we will follow these steps −

  • Define a function gcd() . This will take a, b
  • if a is same as b, then
    • return a
  • otherwise when a < b, then
    • return gcd(b, a)
  • otherwise,
    • return gcd(b, a - b)

Example

Let us see the following implementation to get better understanding −

def gcd(a, b):
   if a == b:
      return a
   elif a < b:
      return gcd(b, a)
   else:
      return gcd(b, a - b)

a = 25
b = 45
print(gcd(a, b))

Input

25, 45

Output

5

Gcd using recursion in python

Updated on 12-Oct-2021 08:11:17

  • Related Questions & Answers
  • Java Program to Find GCD of two Numbers
  • Find GCD of two numbers
  • GCD of more than two (or array) numbers in Python Program
  • Python Program for GCD of more than two (or array) numbers
  • Program to find GCD or HCF of two numbers in C++
  • Java program to find the GCD or HCF of two numbers
  • GCD and LCM of two numbers in Java
  • C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
  • C++ Program for GCD of more than two (or array) numbers?
  • Java Program for GCD of more than two (or array) numbers
  • C++ Program for GCD 0.of more than two (or array) numbers?
  • Program to find GCD or HCF of two numbers using Middle School Procedure in C++
  • Program to find GCD of floating point numbers in C++
  • C program to find GCD of numbers using recursive function
  • Design a TM to compute addition of two unary numbers

What is gcd using recursion?

The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them. For example: Let's say we have following two numbers: 45 and 27 63 = 7 * 3 * 3 42 = 7 * 3 * 2 So, the GCD of 63 and 42 is 21. A program to find the GCD of two numbers using recursion is given as follows.

Is there a gcd function in Python?

Greatest common divisor or gcd is a mathematical expression to find the highest number which can divide both the numbers whose gcd has to be found with the resulting remainder as zero. It has many mathematical applications. Python has a inbuilt gcd function in the math module which can be used for this purpose.

How do you find LCM using recursion in Python?

Python Program to Find the LCM of Two Numbers using Recursion.
Take two numbers from the user..
Initialize the multiple variable with the maximum value among two given numbers..
Check whether the multiple variable clearly divides both the number or not..
If it does, then end the process and return the multiple as the LCM..

Can recursion be used in Python?

Python also accepts function recursion, which means a defined function can call itself. Recursion is a common mathematical and programming concept. It means that a function calls itself.