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

print("Enter 'x' for exit.");
print("Enter two numbers to find HCF and LCM: ");
num1 = input();
if num1 == 'x':
    exit();
else:
    num2 = input();
    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);

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

find hcf lcm python

Above is the initial output. Enter any two numbers say 8 and 20 as input and press enter key to find out the HCF and LCM value of the two numbers as shown in the below sample output:

print hcf and lcm python

Below is the same program with run on python shell:

python find hcf and lcm

Program Video with Compile and Run

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