Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 14

Roots of a quadratic equation

CExpression evaluationJNTUH R22 · CS108ES

Aim

To write a C program that reads the coefficients a, b and c of the quadratic equation ax² + bx + c = 0 and finds its roots, whether they are real and distinct, real and equal, or complex.

Algorithm

  1. Read a, b and c.
  2. If a is 0, the equation is not quadratic. Print a message and stop.
  3. Find the discriminant d = b² − 4ac.
  4. If d > 0, the roots are real and distinct: (−b + √d) / 2a and (−b − √d) / 2a.
  5. If d = 0, the roots are real and equal: −b / 2a.
  6. If d < 0, the roots are complex: the real part is −b / 2a and the imaginary part is √(−d) / 2a.
  7. Print the roots.

Program

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double a, b, c;
    printf("Enter coefficients a, b and c: ");
    if (scanf("%lf %lf %lf", &a, &b, &c) != 3) {
        printf("Invalid input\n");
        return 1;
    }
    if (a == 0) {
        printf("a cannot be 0 in a quadratic equation\n");
        return 0;
    }

    double d = b * b - 4 * a * c;
    if (d > 0) {
        double r1 = (-b + sqrt(d)) / (2 * a);
        double r2 = (-b - sqrt(d)) / (2 * a);
        printf("Roots are real and distinct\n");
        printf("Root 1 = %.2f\nRoot 2 = %.2f\n", r1, r2);
    } else if (d == 0) {
        double r = -b / (2 * a);
        printf("Roots are real and equal\n");
        printf("Root 1 = Root 2 = %.2f\n", r);
    } else {
        double real = -b / (2 * a);
        double imag = sqrt(-d) / (2 * a);
        printf("Roots are complex\n");
        printf("Root 1 = %.2f + %.2fi\nRoot 2 = %.2f - %.2fi\n", real, imag, real, imag);
    }
    return 0;
}

Output

Real and distinct roots

Output 1
Enter coefficients a, b and c: 1 -5 6
Roots are real and distinct
Root 1 = 3.00
Root 2 = 2.00

Real and equal roots

Output 2
Enter coefficients a, b and c: 1 2 1
Roots are real and equal
Root 1 = Root 2 = -1.00

Complex roots

Output 3
Enter coefficients a, b and c: 1 2 5
Roots are complex
Root 1 = -1.00 + 2.00i
Root 2 = -1.00 - 2.00i

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

sqrt is declared in math.h. On Linux, link the maths library by adding -lm at the end of the compile command: gcc quad.c -o quad -lm.

Use double rather than int for the coefficients: the roots are usually fractions, and b * b - 4 * a * c can overflow an int for large values.

Common mistakes

  • Writing -b + sqrt(d) / 2 * a. Division and multiplication happen before addition and from left to right, so this is not the formula. Put brackets around the numerator and the denominator: (-b + sqrt(d)) / (2 * a).

  • Calling sqrt on a negative discriminant. It returns nan (not a number). Check d < 0 first and use sqrt(-d) for the imaginary part.

  • Forgetting -lm on Linux, which gives the linker error undefined reference to sqrt.

  • Reading a double with %f in scanf. A double needs %lf in scanf, though %f is correct in printf.

Viva questions

Try to answer each one aloud before you open it.

  • What is the discriminant, and what does it tell you?

    The discriminant is b² − 4ac. If it is positive the equation has two different real roots, if it is zero the two roots are real and equal, and if it is negative the roots are a pair of complex numbers.

  • Why does the program check whether a is 0?

    If a is 0 the equation becomes bx + c = 0, which is linear, not quadratic. The formula would also divide by 2a, which is division by zero.

  • Which header file is needed for sqrt, and what does -lm do?

    sqrt is declared in math.h. On Linux the maths functions live in a separate library, and -lm tells the linker to include it.

  • Why are the coefficients declared as double and not int?

    Roots are usually fractional, and integer division would cut off the fraction. double also handles a much larger range of values.

  • What are the complex roots of x² + 2x + 5 = 0?

    The discriminant is 4 − 20 = −16, so the roots are −1 + 2i and −1 − 2i.

Learn the concepts

All Programming for Problem Solving Lab experiments →