Algorithm


  1. Input coefficients: Accept the coefficients of the quadratic equation (a, b, c) from the user or any other source.

  2. Calculate discriminant: Calculate the discriminant using the formula discriminant=�2−4��.

    Check discriminant:

    • If the discriminant is positive, there are two real solutions.
    • If the discriminant is zero, there is one real solution.
    • If the discriminant is negative, there are no real solutions (complex solutions).
    • Screenshot-2023-11-30-164003
  3. Output the solutions: Display the solutions to the user.

 

Code Examples

#1 Code Example- Solve Quadratic Equation

Code - Python Programming

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
import cmath

a = 1
b = 5
c = 6

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)
Advertisements

Demonstration


Python Programing Example to Solve Quadratic Equation-DevsEnv