- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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)

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

25, 45

5

- 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++
- Design a TM to compute addition of two unary numbers
- C program to find GCD of numbers using recursive function

Advertisements