8. Square Root
by CodeChum Admin
You've learned in the past lessons on how to use square root functions, right? Then let's take your knowledge to the test. Print out the square root of a given integer with only two decimal places.
Ready, solve, and go!
Input
A line containing an integer.
4
Output
A line containing a decimal with two decimal places.
2.00
import math
integer_number = int(input("Enter an integer: "))
#Check whether the number is less than 0
if integer_number < 0:
print("can't calculate the square root of a negative number")
else:
#find the square root using the square root function in cmath
square_root = math.sqrt(integer_number)
#format to 2dps
square_root = format(square_root, '.2f')
#print the square root
print("The square root of "+str(integer_number)+" is "+str(square_root))
Comments
Leave a comment