Write a program to input two unequal numbers n check weather they are perfect square or not . If the user enters a negative number then the program displays a message .'Square root of a negative number can't be determined.:
Plssss help...
Answers
To figure out the square roots of any number,we import a module called as math and then import sqrt function from the math module:
from math import sqrt
Now, what this sqrt does, it is roots a number given to it. One thing to keep in mind sqrt function works even on non-perfect squares.
But the output would be decimals. For example,
sqrt(2) = 1.4142135623730951
sqrt(3) = 1.7320508075688772
sqrt(4) = 2.0
So, to find whether a number is a perfect square or not, we can convert the decimal into an integer and check whether both of them are equal.
Here is how you do it:
sqrt(4) = 2.0
First, we convert that into an integer, int(2.0) would be 2 and see whether both of them are equal.
2.0 = 2 Hence, the number is a perfect square.
sqrt(3) = 1.7320508075688772
Converting that into an integer, we would have 1
Then, we equate them.
1.7320508075688772 ≠ 1
Hence, we print out that the number is a perfect square.
But, what if our number is negative number?
sqrt(negative number) would give us an error. To not have this error, we check for negative numbers in he start if the number is positive, it proceeds further, else, we print out the message square root of a negative number can't be determined.
CODE:
numbers = [int(input()),int(input())]
for x in numbers:
if x < 1 :
print('Square root of a negative number can't be determined.')
else:
if sqrt(x) == int(sqrt(x)):
print(f'{x} is a perfect square')
else:
print(f'{x} is not a [erfect square')
Answer:
Your answer is in the ATTACHMENT