Site icon Student Projects Live

Python Program to Find the Square Root

Python Program to Find the Square Root

The provided Python program is designed to calculate the square root of a given number. It utilizes the math module, which provides the sqrt function for square root calculations. The program defines a function named find_square_root that takes a numeric input, checks if it is non-negative, and calculates the square root using the math.sqrt function. If the input is negative, the function returns an error message indicating that the square root of a negative number is undefined. In the main part of the program, it prompts the user to enter a number, converts the input to a float, and then calls the find_square_root function with the user-provided value. The result is printed, displaying the square root of the input if it is a non-negative number or an error message for negative inputs. This program provides a straightforward example of how to find the square root in Python, showcasing basic input handling and modular programming.


Source code

import math

def find_square_root(number):
    # Check if the number is non-negative
    if number >= 0:
        # Calculate the square root using math.sqrt()
        square_root = math.sqrt(number)
        return square_root
    else:
        # If the number is negative, return an error message
        return "Cannot calculate square root of a negative number."

# Example usage
if __name__ == "__main__":
    # Get user input for the number
    user_input = float(input("Enter a number: "))
    
    # Call the function and print the result
    result = find_square_root(user_input)
    print(f"The square root of {user_input} is: {result}")

Description

  1. Import the math Module: import math The program begins by importing the math module, which contains various mathematical functions, including sqrt for calculating square roots.
  2. Define the find_square_root Function: def find_square_root(number): A function named find_square_root is defined, which takes a single parameter number representing the input value for which the square root needs to be calculated.
  3. Check for Non-Negativity: if number >= 0: Within the function, it checks if the input number is non-negative.
  4. Calculate Square Root and Return Result: square_root = math.sqrt(number) return square_root If the input is non-negative, the square root is calculated using math.sqrt and returned.
  5. Handle Negative Input: else: return "Cannot calculate square root of a negative number." If the input is negative, the function returns an error message indicating that the square root of a negative number is undefined.
  6. Example Usage in the Main Part: if __name__ == "__main__": This block checks if the script is being run as the main program.
  7. User Input and Function Call: user_input = float(input("Enter a number: ")) result = find_square_root(user_input) The program prompts the user to enter a number, converts the input to a float, and then calls the find_square_root function with the user-provided value.
  8. Print Result: print(f"The square root of {user_input} is: {result}") Finally, the program prints the result, displaying the square root of the entered number if it is non-negative, or an error message if the input is negative.

This program demonstrates a modular approach to finding the square root in Python, incorporating input handling and the use of the math module for mathematical calculations.


Screenshot


Download

Exit mobile version