Articles

# Program to find hcf of two numbers in Python

In this python program guide, we will learn to write a

• program to find hcf of two numbers in python

### Python program to find HCF of two numbers

The python program to find hcf of two numbers is as follows:

``````# Owner : TutorialsInhand Author : Devjeet Roy

n1 = int(input("Enter the first number: "))
n2 = int(input("Enter the second number: "))

while n2:
n1, n2 = n2, n1 % n2

print("The HCF/GCD is:",n1)``````

The output of the python program to find hcf of two numbers is as follows:

``````PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the first number: 54
Enter the second number: 24
The HCF/GCD is: 6``````

1. To find the HCF or GCD of two numbers here, we are using the Euclidean algorithm here.

2. While the remainder is not becoming equal to zero, we keep on following the steps.

3. We are printing the HCF or GCD at the end.

Given below is screenshot of write a program to find hcf of two numbers in python. Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com 