How to calculate HCF using Python3

How to calculate HCF using Python

In this Tutorial, we will find out how to calculate the HCF using Python3.

But, First we need to know that -- 

What is HCF?

HCF (Highest Common Factor) is the highest factor that is common in 2 or more numbers.

What is a Factor?

A Factor is a number that can divide the given number as whole.

Algorithm behind finding the HCF - 

Get two numbers from the user to calculate the HCF.

Calculate the factors of the two numbers.

Now, compare the factors for common ones and write down the largest one.

Sounds Simple ? Let's get started.

n1 = int(input("Enter First Number: "))

n2 = int(input("Enter Second Number: "))

l = []

for i in range(1,n1):

  if ((n1 % i) and (n2 % i)) == 0:

    l.append(i)

print (max(l))

0 Comments