Articles

Program to find hcf of two numbers in Python

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

Few important points about this program:

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.

 

hcf or gcd of two numbers in python

 


Basic Python Programs

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

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Jan 30,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!