Python program to find the Square root of a number

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

  1. Start
  2. Take input: Prompt the user to enter a number.
  3. Find the square root: Compute the square root of the given number using Python’s sqrt() function from the math module.
  4. Display the result: Print the square root of the number.
  5. 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

  1. Importing the “math” module:
    The program imports Python’s math module, which provides mathematical functions like sqrt() for computing the square root of a number.
  2. Taking input from the user:
    The input() function is used to take a number from the user, and it's converted to a float to allow for both integer and decimal numbers.
  3. Finding the square root:
    The math.sqrt() function is used to compute the square root of the number entered by the user. The result is stored in the variable sqrt_value.
  4. Displaying the output:
    The print() function displays the square root of the input number. An f-string is used to format the output neatly.

Python

3341

705

Related Articles