Linux, OpenSource, Programming And Hacks

Jul 18, 2017

Python Program to find HCF and LCM

Jul 18, 2017
HCF is also called Greatest Common divisor (GCD).
the HCF of two or more number is the largest of all the common factors.

for example:
if we take two numbers, 12 and 18
their common divisor are 1,2,3 and 6. therefore 6 being the biggest is the HCF of 12 and 18.

Here, we are using loop method to find HCF of two numbers. It can also be calculated using recursion.

x,y = input("Enter two integer").split()
x,y = [int(x), int(y)] #convert input string to integers

a = x
b = y

while(b != 0 ):
	t = b
	b = a % b
	a = t

hcf = a
lcm = (x*y)/hcf

print("HCF of %d and %d is %d\n" %(x,y,hcf))
print("LCM of %d and %d is %d\n" %(x,y,lcm))

for python2, replace input function with raw_input

No comments:

Post a Comment