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
- Read a, b and c.
- If a is 0, the equation is not quadratic. Print a message and stop.
- Find the discriminant d = b² − 4ac.
- If d > 0, the roots are real and distinct: (−b + √d) / 2a and (−b − √d) / 2a.
- If d = 0, the roots are real and equal: −b / 2a.
- If d < 0, the roots are complex: the real part is −b / 2a and the imaginary part is √(−d) / 2a.
- Print the roots.
Program
#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
Enter coefficients a, b and c: 1 -5 6
Roots are real and distinct
Root 1 = 3.00
Root 2 = 2.00Real and equal roots
Enter coefficients a, b and c: 1 2 1
Roots are real and equal
Root 1 = Root 2 = -1.00Complex roots
Enter coefficients a, b and c: 1 2 5
Roots are complex
Root 1 = -1.00 + 2.00i
Root 2 = -1.00 - 2.00iRecorded 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
sqrton a negative discriminant. It returnsnan(not a number). Checkd < 0first and usesqrt(-d)for the imaginary part.Forgetting
-lmon Linux, which gives the linker errorundefined reference to sqrt.Reading a
doublewith%finscanf. Adoubleneeds%lfinscanf, though%fis correct inprintf.
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?
sqrtis declared inmath.h. On Linux the maths functions live in a separate library, and-lmtells 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.
doublealso 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.