C Programming Code Examples C > If Else and Switch Case Code Examples find all roots of a quadratic equation using switch case find all roots of a quadratic equation using switch case Write a C program to find all roots of a Quadratic equation using switch case. How to find all roots of a quadratic equation using switch case in C programming. Step by step descriptive logic to find roots of quadratic equation using switch case. Input coefficients of quadratic equation. Store it in some variable say a, b and c. Find discriminant of given equation using formula i.e. discriminant = (b b) - (4 a c). You can also use pow() function to find square of b. Compute the roots based on the nature of discriminant. Switch the value of switch(discriminant 0). The expression (discriminant 0) can have two possible cases i.e. case 0 and case 1. For case 1 means discriminant is positive. Apply formula root1 = (-b + sqrt(discriminant)) (2a); to compute root1 and root2 = (-b - sqrt(discriminant)) (2a); to compute root2. For case 0 means discriminant is either negative or zero. There exist one more condition to check i.e. switch(discriminant < 0). Inside case 0 switch the expression switch(discriminant < 0). For the above nested switch there are two possible cases. Which is case 1 and case 0. case 1 means discriminant is negative. Whereas case 0 means discriminant is zero. Apply the formula to compute roots for both the inner cases. #include <stdio.h> #include <math.h> /* Used for sqrt() */ int main() { float a, b, c; float root1, root2, imaginary; float discriminant; printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): "); scanf("%f%f%f", &a, &b, &c); /* Calculate discriminant */ discriminant = (b * b) - (4 * a * c); /* Compute roots of quadratic equation based on the nature of discriminant */ switch(discriminant > 0) { case 1: /* If discriminant is positive */ root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2); break; case 0: /* If discriminant is not positive */ switch(discriminant < 0) { case 1: /* If discriminant is negative */ root1 = root2 = -b / (2 * a); imaginary = sqrt(-discriminant) / (2 * a); printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f", root1, imaginary, root2, imaginary); break; case 0: /* If discriminant is zero */ root1 = root2 = -b / (2 * a); printf("Two equal and real roots exists: %.2f and %.2f", root1, root2); break; } } return 0; }