Write a Python program that takes a number as input from the user and computes its square root.
Input-Output Examples
plaintext
Example 1:
Input: Enter a number: 16
Output: The square root of 16 is: 4.0
Example 2:
Input: Enter a number: 25
Output: The square root of 25 is: 5.0
Algorithm to find the square root of a number
- Start
- Take input: Prompt the user to enter a number.
- Find the square root: Compute the square root of the given number using Python’s
sqrt()
function from themath
module. - Display the result: Print the square root of the number.
- End
Python Program to find the square root of a number
python
# Python program to find the square root of a number
# Importing the math module to use the sqrt() function
import math
# Take input from the user
num = float(input("Enter a number: "))
# Find the square root using the sqrt() function
sqrt_value = math.sqrt(num)
# Display the result
print(f"The square root of {num} is: {sqrt_value}")
Code Explanation
- Importing the “math” module:
The program imports Python’smath
module, which provides mathematical functions likesqrt()
for computing the square root of a number. - Taking input from the user:
Theinput()
function is used to take a number from the user, and it's converted to afloat
to allow for both integer and decimal numbers. - Finding the square root:
Themath.sqrt()
function is used to compute the square root of the number entered by the user. The result is stored in the variablesqrt_value
. - Displaying the output:
Theprint()
function displays the square root of the input number. An f-string is used to format the output neatly.