🔄📚Intermediate
Find All Roots of Quadratic Equation
Find roots using formula
Finds roots of quadratic equation ax² + bx + c = 0 using the quadratic formula:
x = (-b ± √(b²-4ac)) / 2a
Program Code
quadratic_roots.c
C
1#include <stdio.h>2#include <math.h>34int main() {5 double a, b, c, discriminant, root1, root2;6 7 printf("Enter coefficients (a b c): ");8 scanf("%lf %lf %lf", &a, &b, &c);9 10 discriminant = b * b - 4 * a * c;11 12 if (discriminant > 0) {13 root1 = (-b + sqrt(discriminant)) / (2 * a);14 root2 = (-b - sqrt(discriminant)) / (2 * a);15 printf("Two distinct real roots:\n");16 printf("x1 = %.2f\nx2 = %.2f\n", root1, root2);17 } 18 else if (discriminant == 0) {19 root1 = -b / (2 * a);20 printf("One repeated root: x = %.2f\n", root1);21 } 22 else {23 double realPart = -b / (2 * a);24 double imagPart = sqrt(-discriminant) / (2 * a);25 printf("Complex roots:\n");26 printf("x1 = %.2f + %.2fi\n", realPart, imagPart);27 printf("x2 = %.2f - %.2fi\n", realPart, imagPart);28 }29 30 return 0;31}Output
Enter coefficients (a b c): 1 -7 12
Two distinct real roots:
x1 = 4.00
x2 = 3.00
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials