C Program To Find The Roots Of Quadratic Equation

A quadratic equation has the form ax² + bx + c = 0, where a ≠ 0. Its roots are found with the quadratic formula: x = (−b ± √(b²−4ac)) / 2a. The value under the square root — b²−4ac — is called the discriminant (D) and determines what type of roots exist:

Discriminant Roots Example
D > 0 Two distinct real roots x² − 5x + 6 = 0 → 3, 2
D = 0 One repeated real root x² − 4x + 4 = 0 → 2, 2
D < 0 Two complex conjugate roots x² + x + 1 = 0 → −0.5 ± 0.866i

The original post had no code — just a Facebook share button and a brief description. This rewrite covers all three discriminant cases, uses double for precision, and shows the complex root form.

C Program: Roots of a Quadratic Equation

/* Roots of a quadratic equation ax^2 + bx + c = 0
 * Uses the discriminant D = b^2 - 4ac:
 *   D > 0: two distinct real roots
 *   D = 0: one repeated real root
 *   D < 0: two complex conjugate roots
 * Compile: gcc -ansi -Wall -Wextra quadratic.c -o quadratic -lm */
#include <stdio.h>
#include <math.h>

int main(void)
{
    double a, b, c, d, r1, r2;

    printf("Enter coefficients a, b, c (ax^2 + bx + c = 0):\n");
    if (scanf("%lf %lf %lf", &a, &b, &c) != 3) {
        printf("Invalid input.\n");
        return 1;
    }
    if (a == 0.0) {
        printf("Coefficient a cannot be zero (not a quadratic).\n");
        return 1;
    }

    d = b * b - 4.0 * a * c;  /* discriminant */

    if (d > 0.0) {
        r1 = (-b + sqrt(d)) / (2.0 * a);
        r2 = (-b - sqrt(d)) / (2.0 * a);
        printf("Two distinct real roots:\n");
        printf("  root1 = %.4f\n", r1);
        printf("  root2 = %.4f\n", r2);
    } else if (d == 0.0) {
        r1 = -b / (2.0 * a);
        printf("One repeated real root:\n");
        printf("  root = %.4f\n", r1);
    } else {
        double real_part = -b / (2.0 * a);
        double imag_part = sqrt(-d) / (2.0 * a);
        printf("Two complex conjugate roots:\n");
        printf("  root1 = %.4f + %.4fi\n", real_part, imag_part);
        printf("  root2 = %.4f - %.4fi\n", real_part, imag_part);
    }

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra quadratic.c -o quadratic -lm
./quadratic

Sample Output — Three Cases

Case 1: D > 0 — x² − 5x + 6 = 0
Enter coefficients a, b, c: 1 -5 6
Two distinct real roots:
  root1 = 3.0000
  root2 = 2.0000

Case 2: D = 0 — x² − 4x + 4 = 0
Enter coefficients a, b, c: 1 -4 4
One repeated real root:
  root = 2.0000

Case 3: D < 0 — x² + x + 1 = 0
Enter coefficients a, b, c: 1 1 1
Two complex conjugate roots:
  root1 = -0.5000 + 0.8660i
  root2 = -0.5000 - 0.8660i

How the Discriminant Works

For x² − 5x + 6: D = (−5)² − 4(1)(6) = 25 − 24 = 1. Since D > 0, two real roots: (5 ± √1) / 2 = 3 and 2. Verify: (x−3)(x−2) = x² − 5x + 6 ✓

For x² + x + 1: D = 1² − 4(1)(1) = 1 − 4 = −3. Since D < 0, complex roots: real = −1/2, imag = √3/2 ≈ 0.8660.

Code Explanation

  • Discriminant first — compute d = b*b - 4.0*a*c before calling sqrt. Never call sqrt(d) when d < 0 — that gives NaN on most systems (undefined behavior in strict math, domain error). The if-else chain ensures sqrt is only called with a non-negative argument.
  • Complex roots: real part and imaginary part — when D < 0, sqrt(-d) gives the magnitude of the imaginary part. The real part is -b/(2a) for both roots. The two roots are complex conjugates: real + imag·i and real − imag·i.
  • Check a == 0.0 — if a is zero, it is not a quadratic at all (it becomes linear: bx + c = 0). Division by 2a with a=0 would cause division by zero. Always validate the leading coefficient.
  • Use double not float — quadratic roots can be very sensitive to precision. With float (~7 significant digits), nearly-equal roots (D close to 0) can suffer severe cancellation error. Double gives ~15 digits and is what sqrt() returns.
  • %lf in scanf for double — in C89/C90, scanf("%lf", &x) is required for double. Using %f would read into a float, causing a type mismatch. (In C99+, %f and %lf are equivalent for scanf, but %lf is still preferred for clarity.)

What This Program Teaches

  • The three discriminant cases — D>0, D=0, D<0 are a clean example of branching on a computed value. Quadratic equations are one of the first places students encounter the concept of complex numbers arising naturally from real-coefficient equations.
  • Never call sqrt with a negative argument — in C, sqrt(negative) sets errno to EDOM and returns NaN. Always check the sign before calling. This pattern — compute discriminant first, then branch — is the canonical safe form.
  • Numerical stability consideration — for roots very close together (D near zero), the subtraction -b - sqrt(d) can lose precision (catastrophic cancellation). The numerically stable form uses the sign of b: compute the root that avoids cancellation first, then use Vieta’s formula (root1 × root2 = c/a) to get the other.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

1 comment on “C Program To Find The Roots Of Quadratic Equation

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>