Write a program that has a user defined function to accept the coefficients of a quadratic equation in variables and calculates its determinant. For example: if the coefficients are stored in the variables a,b,c then calculate determinant as -4ac. Write the appropriate condition to check determinants on positive, zero and negative and output appropriate result.
Program to calculate determinant of a quadratic equation
and check nature of roots
User defined function to calculate determinant
def calculate_determinant(a, b, c): # Calculate determinant using formula - 4ac determinant = b**2 - 4ac return determinant
Accept coefficients from user
a = float(input("Enter coefficient a: ")) b = float(input("Enter coefficient b: ")) c = float(input("Enter coefficient c: "))
Call the function to calculate determinant
det = calculate_determinant(a, b, c)
Check determinant and output appropriate result
if det > 0: print("Determinant is positive. Roots are real and distinct.") elif det == 0: print("Determinant is zero. Roots are real and equal.") else: print("Determinant is negative. Roots are imaginary.")
Explanation
This program demonstrates the use of user-defined functions in Python. The function calculate_determinant() accepts three parameters a, b, c representing the coefficients of a quadratic equation ax² + bx + c = 0. It calculates the determinant using the formula b² - 4ac and returns the value. The main program accepts user input for the coefficients, calls the function, and uses conditional statements (if-elif-else) to check whether the determinant is positive, zero, or negative, displaying appropriate messages for each case.
Solution Steps
Step 1: Define a user-defined function calculate_determinant() that accepts three parameters a, b, c
Step 2: Inside the function, calculate determinant using formula b² - 4ac and return the value
Step 3: Accept coefficients a, b, c as input from the user
Step 4: Call the function and store the returned determinant value
Step 5: Use if-elif-else conditions to check if determinant is positive, zero, or negative and print appropriate result