Write a program to ask the user for 2 Numbers and print close if the number are within .001 of each other and not close otherwise.
Python
Answers
CODE :-
n1 = int(input("Enter a number : "))
n2 = int(input("Enter another number : "))
if (n1-n2<=0.001) or (n2-n1<=0.001):
print("Close")
else:
print("Not close")
Hope this helps.....
Answer:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if abs(num1 - num2) <= 0.001:
print("Close")
else:
print("Not close")
Explanation:
Input:
Enter the first number: 15
Enter the second number: 15.001
Output: Close
The program takes two numbers from the user and then prints 'Close' if the difference between two numbers* is less or equals to 0.001, Otherwise it prints 'Not close'.
* The difference will always convert into positive value, because of the 'abs' function. 'abs' is a built-in math function in python which is used for absolute value. It is a simple if-else problem. I hope you understood it.