C Program to Solve Quadratic Equation with Output
discriminant = b*b - 4*a*c and calculates roots with root1 = (-b + sqrt(discriminant)) / (2*a) and root2 = (-b - sqrt(discriminant)) / (2*a) depending on the discriminant value.Examples
How to Think About It
b*b - 4*a*c. If the discriminant is positive, roots are two different real numbers. If zero, roots are equal real numbers. If negative, roots are complex numbers with real and imaginary parts.Algorithm
Code
#include <stdio.h> #include <math.h> int main() { double a, b, c, discriminant, root1, root2, realPart, imagPart; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); if (a == 0) { printf("Coefficient 'a' cannot be zero for a quadratic equation.\n"); return 1; } discriminant = b*b - 4*a*c; if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2*a); root2 = (-b - sqrt(discriminant)) / (2*a); printf("Roots are real and different: %.2lf and %.2lf\n", root1, root2); } else if (discriminant == 0) { root1 = -b / (2*a); printf("Roots are real and same: %.2lf and %.2lf\n", root1, root1); } else { realPart = -b / (2*a); imagPart = sqrt(-discriminant) / (2*a); printf("Roots are complex and different: %.2lf + %.2lfi and %.2lf - %.2lfi\n", realPart, imagPart, realPart, imagPart); } return 0; }
Dry Run
Let's trace the input a=1, b=-3, c=2 through the code
Input coefficients
a=1, b=-3, c=2
Calculate discriminant
discriminant = (-3)*(-3) - 4*1*2 = 9 - 8 = 1
Check discriminant > 0
1 > 0 is true, so roots are real and different
Calculate roots
root1 = (-(-3) + sqrt(1)) / (2*1) = (3 + 1)/2 = 2 root2 = (3 - 1)/2 = 1
Print roots
Roots are real and different: 2.00 and 1.00
| Step | Variable | Value |
|---|---|---|
| 2 | discriminant | 1 |
| 4 | root1 | 2 |
| 4 | root2 | 1 |
Why This Works
Step 1: Calculate discriminant
The discriminant b*b - 4*a*c tells us the nature of roots: positive means two real roots, zero means one real root repeated, negative means complex roots.
Step 2: Compute roots based on discriminant
If discriminant is positive or zero, roots are calculated using the quadratic formula with square root of discriminant. If negative, roots have real and imaginary parts.
Step 3: Print roots
Roots are printed with two decimal places for clarity, showing whether they are real and different, real and same, or complex.
Alternative Approaches
#include <stdio.h> #include <complex.h> int main() { double a, b, c; double complex root1, root2; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); if (a == 0) { printf("Coefficient 'a' cannot be zero for a quadratic equation.\n"); return 1; } double complex discriminant = csqrt(b*b - 4*a*c); root1 = (-b + discriminant) / (2*a); root2 = (-b - discriminant) / (2*a); printf("Roots are: %.2lf%+.2lfi and %.2lf%+.2lfi\n", creal(root1), cimag(root1), creal(root2), cimag(root2)); return 0; }
#include <stdio.h>
#include <math.h>
double discriminant(double a, double b, double c) {
return b*b - 4*a*c;
}
void findRoots(double a, double b, double c) {
if (a == 0) {
printf("Coefficient 'a' cannot be zero for a quadratic equation.\n");
return;
}
double disc = discriminant(a,b,c);
if (disc > 0) {
printf("Roots are real and different: %.2lf and %.2lf\n", (-b+sqrt(disc))/(2*a), (-b-sqrt(disc))/(2*a));
} else if (disc == 0) {
printf("Roots are real and same: %.2lf and %.2lf\n", -b/(2*a), -b/(2*a));
} else {
double real = -b/(2*a);
double imag = sqrt(-disc)/(2*a);
printf("Roots are complex and different: %.2lf + %.2lfi and %.2lf - %.2lfi\n", real, imag, real, imag);
}
}
int main() {
double a,b,c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
findRoots(a,b,c);
return 0;
}Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of arithmetic operations and conditional checks, so it runs in constant time O(1).
Space Complexity
Only a few variables are used to store inputs and results, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time, but using
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic if-else with math.h | O(1) | O(1) | Simple and portable code |
| Using complex.h library | O(1) | O(1) | Automatic complex root handling, requires C99 |
| Separate functions | O(1) | O(1) | Improved readability and modularity |