C Program to Find the Roots of a Quadratic Equation

To understand this example, you should have the knowledge of the following C programming topics:


The standard form of a quadratic equation is:

ax2 + bx + c = 0, where
a, b and c are real numbers and
a != 0

The term b2; - 4ac is known as the discriminant of a quadratic equation. It tells the nature of the roots.

  • If the discriminant is greater than 0, the roots are real and different.
  • If the discriminant is equal to 0, the roots are real and equal.
  • If the discriminant is less than 0, the roots are complex and different.
Formula to compute the roots of a quadratic equation
Figure: Roots of a Quadratic Equation

Program to Find Roots of a Quadratic Equation

Output

Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i

In this program, the sqrt() library function is used to find the square root of a number. To learn more, visit: sqrt() function.