Program to Find HCF in Python

Program to Find HCF in Python Programming language. This program contains if else condition, input, print functions and for loop. This program is for Beginners, class 11,12 and B Tech computer science students.

Program to Find HCF in Python

HCF: Highest Common Factor

Highest Common Factor or Greatest Common Divisor of two or more integers when at least one of them is not zero is the largest positive integer that evenly divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.
We have two integers 8 and 12. Let’s find the HCF.
The divisors of 8 are:
1, 2, 4, 8  
The divisors of 12 are:
1, 2, 3, 4, 6, 12  
HCF /GCD is the greatest common divisor. So HCF of 8 and 12 are 4.

Source code:

def calculate_hcf(x, y):  
      
    if x > y:  
        smaller = y  
    else:  
        smaller = x  
    for i in range(1,smaller + 1):  
        if((x % i == 0) and (y % i == 0)):  
            hcf = i  
    return hcf  
  
num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))  
print("The H.C.F. of", num1,"and", num2,"is", calculate_hcf(num1, num2))  

Download Source code : For Download Click on the below Button

Step wise Source Code Explanation :

Step 1 : Defining a function to calculate HCF
def calculate_hcf(x, y):
Step 2 : Selecting the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
Step 3 : Taking input from users
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
Step 4 : printing the result for the users
print(“The H.C.F. of”, num1,”and”, num2,”is”, calculate_hcf(num1, num2))

Output :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Enter first number: 12
Enter second number: 18
The H.C.F. of 12 and 18 is 6
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Enter first number: 25
Enter second number: 50
The H.C.F. of 25 and 50 is 25
>>> 

Output Explanation :

First number to calculate HCF is entered.
Enter first number: 25
Second number to calculate HCF is entered.
Enter second number: 50
The HCF is calculated and printed.
The H.C.F. of 25 and 50 is 25

Conclusion:

The program is running fine.

Thank you for learning and reading the Program to Find HCF in Python post. This program contains if else condition, input, print functions and for loop. This program is for Beginners, class 11,12 and B Tech computer science students. For more Python coding post, practical file of class 11 and 12 link is provided below :

Python Programs Archives – CS Study

Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study

Computer Science Practical file for class 12 Term-2 – CS Study

Leave a Comment

Your email address will not be published.