Program to compute gcd of two numbers recursively in Python

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
  • 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 

    Input

    25, 45
    

    Output

    5
    Updated on: 2021-10-12T08:11:17+05:30

    8K+ Views

    Kickstart Your Career

    Get certified by completing the course

    Get Started
    Advertisements