Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I composed the accompanying project in python to discover hcf and lcm of two numbers a and b. x is the more prominent of the two numbers and y is more modest, the two of which I expect to discover in the upper piece of the program. they will be utilized later for discovering HCF and LCM. however, when I run it, it conceals x in red. I cannot comprehend the explanation.

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 algorithm of discovering lcm, HCF works totally in c, so I don't feel there ought to be an issue with the algorithm. it very well maybe some punctuation issue.

closed

4 Answers

0 votes
by (19k points)
 
Best answer
a, b = map(int, input("Enter two numbers separated by a space: ").split())

x, y = (a, b) if a > b else (b, a)

hcf = max(i for i in range(1, x) if a % i == 0 and b % i == 0)

lcm = min(j for j in range(x, a * b) if j % a == 0 and j % b == 0)

print("HCF:", hcf)

print("LCM:", lcm)

In this, the map() function is used to convert the input numbers to integers. The variables x and y are assigned using a conditional expression. The max() function is used with a generator expression to find the highest common factor (HCF), and the min() function with a generator expression is used to find the least common multiple (LCM). Finally, the print statements display the results.
0 votes
by (26.4k points)

Try the below code:

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))

Want to become an expert in Python? Join the python course fast!

To know more about this you can have a look at the following video tutorial:-

0 votes
by (25.7k points)
There are a few issues with the code you provided. Let's go through them and make the necessary corrections:

The raw_input() function is not available in Python 3. Instead, you can use the input() function to get user input.

When assigning values to variables x and y, you are using incorrect syntax. In Python, you don't need to specify the type explicitly. Also, the correct assignment syntax is = instead of ==.

The logical operator for "and" in Python is and, not &.

You need to initialize the variables hcf and lcm before using them in the print statements.

You need to convert the inputs a and b to integers using the int() function.

Here's the corrected code:

a, b = input("Enter two numbers (with a space in between): ").split()

a = int(a)

b = int(b)

if a > b:

    x = a

    y = b

else:

    x = b

    y = a

hcf = 0

for i in range(1, x):

    if a % i == 0 and b % i == 0:

        hcf = i

print("HCF of both numbers is:", hcf)

lcm = 0

for j in range(x, a * b):

    if j % a == 0 and j % b == 0:

        lcm = j

print("LCM of both numbers is:", lcm)

With these corrections, the code should work as intended.
0 votes
by (15.4k points)
Here is the correct code:

a, b = input("Please enter two numbers (separated by a space): ").split()

a = int(a)

b = int(b)

if a > b:

    x = a

    y = b

else:

    x = b

    y = a

hcf = 0

for i in range(1, x):

    if a % i == 0 and b % i == 0:

        hcf = i

print("The highest common factor (HCF) of both numbers is:", hcf)

lcm = 0

for j in range(x, a * b):

    if j % a == 0 and j % b == 0:

        lcm = j

print("The least common multiple (LCM) of both numbers is:", lcm)

In this updated version of the code, the input prompt has been rephrased to provide clearer instructions. The variables a and b are now correctly converted to integers using the int() function. The assignments of x and y have been adjusted based on the values of a and b. The logical operator and has been used instead of & for checking the conditions. Additionally, the variables hcf and lcm have been initialized to 0 before their respective loops. Finally, the print statements have been modified to provide meaningful output.

Related questions

0 votes
4 answers
asked Apr 4, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...