PYTHON PROGRAM THAT ASKS USER FOR TWO NUMBERS AND PRINTS CLOSE IF NUMBERS ARE WITHIN .001 OF EACH OTHER AND NOT CLOSE OTHERWISE
Write a program that asks the user for two numbers and prints Close if the numbers are within .001 of each other and Not close otherwise.
CODE:
a=float(input("Enter a-value:"))
b=float(input("Enter b-value:"))
if abs(a-b)<=0.001:
print("Close")
else:
print("Not close")
EXPLANATION:
In this code, the program asks the user to enter two numbers a and b (a>b or b>a)
It finds their difference and returns an appropriate message if their difference is within 0.001(<=0.001).
Here we use abs() function to find their absolute difference.
Absolute value of a number is the value without considering its sign. Hence absolute of 10 is 10, -10 is also 10. If the number is a complex number, abs() returns its magnitude.
OUTPUT:
Comments
Post a Comment