Skip to study material
← Question index

QUESTION 26 · Worked algorithms

Develop an algorithm and flowchart to find the roots of a quadratic equation.

5 marks

Answer in brief

For the equation ax² + bx + c = 0, where a is not zero, the roots are found using the discriminant D = b² − 4ac.

Types of roots

  1. D > 0: There are two different real roots: (−b + √D)/(2a) and (−b − √D)/(2a).
  2. D = 0: There is one repeated real root: −b/(2a).
  3. D < 0: There are two complex roots. Their real part is −b/(2a), and their imaginary magnitude is √(−D)/|2a|. The pair is written as real part ± imaginary magnitude × i.

Algorithm

  1. Read a, b and c: If a is zero, report that the equation is not quadratic.
  2. Calculate D: Use D = b² − 4ac.
  3. Choose the correct formula: Test whether D is positive, zero or negative.
  4. Display the roots: Show the real roots or the complex pair as required.

Example

Let a = 1, b = −3 and c = 2. Then D = 9 − 8 = 1. The roots are (3 + 1)/2 = 2 and (3 − 1)/2 = 1. Checking them in the equation confirms the result: 2² − 3(2) + 2 = 0 and 1² − 3(1) + 2 = 0.

Important points

Check a before dividing by 2a. Do not use the real square-root function on a negative D. In the complex case, calculate √(−D). The program uses small fixed coefficients for a clear example. Floating-point rounding can affect values very close to zero, and the basic formula can lose accuracy for extreme inputs. With GCC, mathematical functions may require the -lm option when linking.

Time and space complexity

The method uses a fixed number of calculations and comparisons. Under the usual fixed-width analysis, time and extra space are O(1).

Reference table

↔ Scroll horizontally on a small screen
Boundary checks
CaseExpected behaviour
a = 0Reject as not quadratic.
D = 0Report the repeated root once.
D < 0Report complex conjugates, not a real sqrt of a negative value.

Diagram or flowchart

Validate the leading coefficient, compute the discriminant, select one of three root cases and finish.
Classifying quadratic roots · Download diagram

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
#include <math.h>
int main(void)
{
    double a = 1.0, b = -3.0, c = 2.0;
    if (a == 0.0) { puts("Not quadratic"); return 1; }
    double d = b*b - 4.0*a*c;
    if (d > 0.0) {
        printf("Roots = %.2f and %.2f\n", (-b+sqrt(d))/(2*a), (-b-sqrt(d))/(2*a));
    } else if (d == 0.0) {
        printf("Repeated root = %.2f\n", -b/(2*a));
    } else {
        printf("Roots = %.2f +/- %.2fi\n", -b/(2*a), sqrt(-d)/fabs(2*a));
    }
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

Roots = 2.00 and 1.00

Key points

  • For ax² + bx + c = 0 with a non-zero, compute D = b² − 4ac; the sign of D determines whether the roots are distinct real, repeated real or complex.
  • Check the discriminant before applying a real square root.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • June 2022 · Q8(a) · 7 marks

    Write an algorithm to find the roots of a quadratic equation.

    Paper P03 · page 1
  • July 2021 · Q8(a) · 7 marks

    Write an algorithm for finding the roots of a quadratic equation.

    Paper P04 · page 1
  • March/April 2023 · Q3(a) · 4 marks

    Write an algorithm to find the roots of a quadratic equation.

    Paper P05 · page 1
View all matching exam references →