Python Program to Find Square Root of Number.

Calculating the square root of a number is a common mathematical operation. In Python, there are multiple ways to find the square root, offering flexibility based on the requirements of your program. In this article, we have discussed different approaches to calculating the square root of a given number.


Problem Statement: Write a Python program to find the square root of a given number.


Approach 1: Using the ** operator.

In Python, the ** operator is used for exponentiation, meaning it raises the left operand to the power of the right operand.


Below are the steps of the Algorithm to follow:

  • STEP 1: Take user input for the number.
  • STEP 2: Calculate the square root using the ** operator.
  • STEP 3: Display the result.

Python Code Implementation.

# Python Program to Find Square Root of a Number using ** Operator

number = float(input("Enter a number: "))

# Calculate square root
result = number ** 0.5

print(f"The square root of {number} is {result}")
Output:
Enter a number: 6
The square root of 6.0 is 2.449489742783178

Approach 2: Using the math.sqrt() Function.

The math.sqrt() function in Python is part of the math module and is used for finding the square root of a given number.

Below are the steps to follow:
  • STEP 1: Import the math module in code.
  • STEP 2: Take user input for the number.
  • STEP 3: Use the math.sqrt() function to calculate the square root.
  • STEP 4: Display the square root of the given number.

Python Code Implementation.

# Python Program to Find Square Root using math.sqrt()
import math

number = float(input("Enter a number: "))

#  Calculate square root
result = math.sqrt(number)

print(f"The square root of {number} is {result}")
Output:
Enter a number: 25
The square root of 25.0 is 5.0

Conclusion:

Both approaches provide a way to find the square root of a number in Python. The first approach uses the ** operator, and the second approach uses the math.sqrt() function. Choose the one that fits your coding style and requirements.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS