Welcome folks today in this blog post we will be finding gcd or hcf of two numbers by euclid's algorithm using recursion
in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# defining gcd function def gcd(num1,num2): if num1%num2 == 0: # Base case return num2 else: # Iterative case return gcd(num2,num1%num2) # taking first number num1 = int(input("Enter first number: ")) # taking second number num2 = int(input("Enter second number: ")) # checking if num2 is greater than num1 then swap these numbers if num2 > num1: (num1,num2) = (num2,num1) # printing GCD print("The GCD of the numbers is",gcd(num1,num2)) |
Now if you execute the python
script by typing the below command as shown below
python app.py