Roots of a Quadratic Equation: flowchart, algorithm and program

This program reads the coefficients a, b and c of a quadratic equation and finds its roots. It first computes the discriminant d = b*b - 4*a*c, and the sign of d decides whether the roots are real and distinct, real and equal, or complex.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read a, b, c.
  3. Step 3: Set d = b*b - 4*a*c.
  4. Step 4: If d > 0, go to step 5; otherwise go to step 7.
  5. Step 5: Set r1 = (-b + sqrt(d)) / (2*a) r2 = (-b - sqrt(d)) / (2*a).
  6. Step 6: Print real & distinct, r1, r2. Go to step 12.
  7. Step 7: If d == 0, go to step 8; otherwise go to step 10.
  8. Step 8: Set r1 = -b / (2*a).
  9. Step 9: Print real & equal, r1. Go to step 12.
  10. Step 10: Set real = -b / (2*a) imag = sqrt(-d) / (2*a).
  11. Step 11: Print complex roots real ± imag i.
  12. Step 12: Stop.

Pseudocode

BEGIN
    READ a, b, c
    d ← b*b - 4*a*c
    IF d > 0 THEN
        r1 ← (-b + sqrt(d)) / (2*a) r2 ← (-b - sqrt(d)) / (2*a)
        PRINT real & distinct, r1, r2
    ELSE
        IF d == 0 THEN
            r1 ← -b / (2*a)
            PRINT real & equal, r1
        ELSE
            real ← -b / (2*a) imag ← sqrt(-d) / (2*a)
            PRINT complex roots real ± imag i
        END IF
    END IF
END

Program in C, Python and Java

Roots of a Quadratic Equation program in C

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

int main() {
    float a, b, c, d, r1, r2, real, imag;
    printf("Enter a, b, c: ");
    scanf("%f %f %f", &a, &b, &c);
    d = b * b - 4 * a * c;
    if (d > 0) {
        r1 = (-b + sqrt(d)) / (2 * a);
        r2 = (-b - sqrt(d)) / (2 * a);
        printf("Roots are real and distinct\n");
        printf("Root1 = %.2f, Root2 = %.2f\n", r1, r2);
    } else if (d == 0) {
        r1 = -b / (2 * a);
        printf("Roots are real and equal\n");
        printf("Root1 = Root2 = %.2f\n", r1);
    } else {
        real = -b / (2 * a);
        imag = sqrt(-d) / (2 * a);
        printf("Roots are complex\n");
        printf("Root1 = %.2f + %.2fi, Root2 = %.2f - %.2fi\n", real, imag, real, imag);
    }
    return 0;
}

Roots of a Quadratic Equation program in Python

import math

a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
d = b * b - 4 * a * c
if d > 0:
    r1 = (-b + math.sqrt(d)) / (2 * a)
    r2 = (-b - math.sqrt(d)) / (2 * a)
    print("Roots are real and distinct")
    print("Root1 = %.2f, Root2 = %.2f" % (r1, r2))
elif d == 0:
    r1 = -b / (2 * a)
    print("Roots are real and equal")
    print("Root1 = Root2 = %.2f" % r1)
else:
    real = -b / (2 * a)
    imag = math.sqrt(-d) / (2 * a)
    print("Roots are complex")
    print("Root1 = %.2f + %.2fi, Root2 = %.2f - %.2fi" % (real, imag, real, imag))

Roots of a Quadratic Equation program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double a, b, c, d, r1, r2, real, imag;
        System.out.print("Enter a, b, c: ");
        a = sc.nextDouble();
        b = sc.nextDouble();
        c = sc.nextDouble();
        d = b * b - 4 * a * c;
        if (d > 0) {
            r1 = (-b + Math.sqrt(d)) / (2 * a);
            r2 = (-b - Math.sqrt(d)) / (2 * a);
            System.out.println("Roots are real and distinct");
            System.out.printf("Root1 = %.2f, Root2 = %.2f\n", r1, r2);
        } else if (d == 0) {
            r1 = -b / (2 * a);
            System.out.println("Roots are real and equal");
            System.out.printf("Root1 = Root2 = %.2f\n", r1);
        } else {
            real = -b / (2 * a);
            imag = Math.sqrt(-d) / (2 * a);
            System.out.println("Roots are complex");
            System.out.printf("Root1 = %.2f + %.2fi, Root2 = %.2f - %.2fi\n", real, imag, real, imag);
        }
    }
}

Sample input: a = 1, b = 5, c = 6
Output: Roots are real and distinct Root1 = -2.00, Root2 = -3.00

How it works

  • Read a, b and c with scanf, then compute the discriminant: d = b * b - 4 * a * c.
  • If d > 0, there are two different real roots: r1 = (-b + sqrt(d)) / (2 * a) and r2 = (-b - sqrt(d)) / (2 * a).
  • If d == 0, both roots are the same, so one value is enough: r1 = -b / (2 * a).
  • If d < 0, the square root of a negative number is not real. So the program splits the roots into real = -b / (2 * a) and imag = sqrt(-d) / (2 * a), and prints them as real + imag i and real - imag i.
  • Dry run with a=1, b=5, c=6: d = 25 - 24 = 1, which is greater than 0. So r1 = (-5 + 1) / 2 = -2 and r2 = (-5 - 1) / 2 = -3.

Dry run (trace table)

StepLineBoxabcdr1r2Output
14Start
27Read a, b, c156Enter a, b, c:
38d = b*b - 4*a*c1561
49d > 0?1561
511r1 = (-b + sqrt(d)) / (2*a) r2 = (-b - sqrt(d)) / (2*a)1561-2-3
613Print real & distinct, r1, r21561-2-3Roots are real and distinct Root1 = -2.00, Root2 = -3.00
724Stop1561-2-3

Time and space complexity

Time: O(1) · Space: O(1). A fixed number of arithmetic operations and one branch are done, independent of input size.

Common mistakes

  • Writing 2 * a without brackets, as in -b + sqrt(d) / 2 * a. The whole numerator and the whole denominator each need their own brackets.
  • Calling sqrt(d) when d is negative. In the complex case you must use sqrt(-d), otherwise you get nan.
  • Forgetting that a must not be 0. If a is 0 the equation is not quadratic and the program divides by zero.

Viva questions

Q1. What is the discriminant, and what does its sign tell you in this program?

The discriminant is d = b*b - 4*a*c. If d > 0 there are two distinct real roots, if d == 0 there is one repeated real root, and if d < 0 the roots are complex.

Q2. Why do you use sqrt(-d) in the else block instead of sqrt(d)?

In that branch d is negative, and sqrt of a negative number is invalid (it gives NaN). So I take the square root of the positive value -d and print it as the imaginary part.

Q3. What happens if the user enters a = 0? How would you fix it?

Then 2*a is 0, so the division is by zero and the output is inf or nan, and it is not a quadratic anyway. I would check a == 0 first and either print an error or solve the linear equation -c/b.

Q4. Why is math.h included, and is anything extra needed when compiling?

math.h declares sqrt(). On some compilers, such as gcc on Linux, you must also link the math library with -lm.

Q5. Is the condition d == 0 reliable with float variables?

Not always. Floats are stored approximately, so d can come out as a tiny nonzero value even when the exact answer is 0. A safer check is fabs(d) < a small epsilon, or using double for more precision.

Q6. Trace the program for a=1, b=-3, c=2. What is printed?

d = 9 - 8 = 1, which is greater than 0. r1 = (3+1)/2 = 2.00 and r2 = (3-1)/2 = 1.00, so it prints 'Roots are real and distinct' and then 'Root1 = 2.00, Root2 = 1.00'.

Q7. In the complex case, why are both roots printed with the same imag value, and what are the two roots?

The complex roots are conjugates: real ± i*imag. Both use the same magnitude of imag, with + for one root and - for the other. If a is negative, imag is negative, so the signs in the printout would look swapped.

Q8. What is the time complexity of this program, and what would change if you used double instead of float?

It is O(1), because it does a fixed number of arithmetic operations and no loops. With double, I would use %lf in scanf (and %f is fine in printf), and the results would be more precise, which also makes the d == 0 check more dependable.

Frequently asked questions

What is the discriminant of a quadratic equation?

It is d = b*b - 4*a*c. If d is positive the roots are real and distinct, if it is zero they are real and equal, and if it is negative they are complex.

Why does the program use sqrt(-d) in the complex case?

When d is negative, sqrt(d) is not a real number. The roots are real part ± i times sqrt(-d) / (2a), so the program takes the square root of the positive value -d and prints the i separately.

What happens if a is 0?

The equation is then linear, and (2 * a) becomes 0, so the program divides by zero. A safer version checks for a == 0 first and handles it separately.

More programs

← All programs

Try AI Flowchart free →