codescracker


python

Python Program to Find HCF and LCM



« Previous Program Next Program »


Find HCF and LCM in Python

To find HCF and LCM of two numbers in python, you have to ask from user to enter any two number to find and print the value of HCF and LCM as output as shown in the program given here.

Python Programming Code to Find HCF and LCM

Following python program ask from user to enter any two number to find and print HCF and LCM of the given two numbers:

# Python Program - Find HCF and LCM
		
while True:
	print("Enter 'x' for exit.")
	print("Enter two numbers: ")
	num1 = input()
	num2 = input()
	if num1 == 'x':
		break
	else:
		number1 = int(num1)
		number2 = int(num2)
		temp1 = number1
		temp2 = number2
		while temp2 != 0:
			t = temp2
			temp2 = temp1%temp2
			temp1 = t
		hcf = temp1
		lcm = (number1*number2)/hcf
		print("HCF =",hcf)
		print("LCM =",lcm,"\n")

Here is the sample run of the above Python program shows how to find HCF and LCM of numbers:

python find hcf and lcm

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test