Q.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 b^2-4ac. Write the appropriate condition to check determinants on positive, zero and negative and output appropriate result.
A user-defined function returns the determinant b**2 − 4ac of the quadratic's coefficients, and an if–elif–else on its sign prints the verdict: positive → two distinct real roots, zero → equal roots, negative → no real roots.
The idea. For a quadratic with coefficients a, b, c, the expression b^2 − 4ac (the determinant/discriminant) decides the nature of the roots before any root is computed. The clean design mirrors that: one function computes the value (a pure calculation, returned to the caller), and the main program classifies it with a three-way selection.
def determinant(a, b, c):
# Return the determinant b^2 - 4ac of the quadratic equation
return b ** 2 - 4 * a * c
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
d = determinant(a, b, c)
print("Determinant =", d)
if d > 0:
print("Positive: the equation has two distinct real roots")
elif d == 0:
print("Zero: the equation has two equal real roots")
else:
print("Negative: the equation has no real roots")
Sample runs
Enter coefficient a: 1
Enter coefficient b: -7
Enter coefficient c: 12
Determinant = 1.0
Positive: the equation has two distinct real roots
Enter coefficient a: 1
Enter coefficient b: 4
Enter coefficient c: 4
Determinant = 0.0
Zero: the equation has two equal real roots
Enter coefficient a: 1
Enter coefficient b: 1
Enter coefficient c: 1
Determinant = -3.0
Negative: the equation has no real roots
Checking the arithmetic
| a | b | c | b**2 | 4ac | Determinant | Verdict |
|---|---|---|---|---|---|---|
| 1 | −7 | 12 | 49 | 48 | 1.0 | two distinct real roots |
| 1 | 4 | 4 | 16 | 16 | 0.0 | two equal real roots |
| 1 | 1 | 1 | 1 | 4 | −3.0 | no real roots |
Key lines
b ** 2 - 4 * a * ctranslates the formula exactly;**is Python's power operator (never writeb^2— in Python^is bitwise XOR, a different operation entirely).- The function returns the value instead of printing, so the caller can both display it and branch on it.
Comparing floats to zero with == is acceptable for textbook inputs, but real-world coefficients may produce values like 1e-16 instead of exactly 0.0 due to float rounding. Also note a must be non-zero for the equation to be quadratic at all — a guard if a == 0: is a worthwhile refinement.
The function computes the determinant as b**2 − 4ac and the program classifies its sign: e.g. (1, −7, 12) → 1.0 → two distinct real roots; (1, 4, 4) → 0.0 → two equal real roots; (1, 1, 1) → −3.0 → no real roots.
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.