Search code examples
pythonpython-2.7lcm

python program to find hcf and lcm


i wrote the following progran in python to find out hcf and lcm of two numbers a and b. x is the greater of the two numbers and y is smaller, both of which i intend to find in upper part of program. they will be used later for finding hcf and lcm.but when i run it, it shades x in red. i cant understand the reason.

a,b=raw_input("enter two numbers (with space in between: ").split()
if (a>b):
    int x==a
else:
    int x==b
for i in range (1,x):
    if (a%i==0 & b%i==0):
        int hcf=i
print ("hcf of both is: ", hcf)
for j in range (x,a*b):
    if (j%a==0 & j%b==0):
        int lcm=j
print ("lcm of both is: ", lcm)        

this algo of finding lcm, hcf works perfectly in c, so i dont feel there should be problem with algo. it might be some syntax problem.


Solution

  • import sys
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    sa = a
    sb = b
    r = a % b
    while r != 0:
        a, b = b, r
        r = a % b
    h = b
    l = (sa * sb) / h
    print('a={},b={},hcf={},lcm={}\n'.format(sa,sb,h,l))