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. Finding all roots of a quadratic equation using switch case.

Input a: 4

Input b: -2

Input c: -10

Output root1: 1.85

Output root2: -1.35

We have already talked about what is Quadratic equation and how to find all roots of quadratic equation using if else. Here I will be explaining how to find roots of quadratic equation using switch case.

Happy coding ;)

**Example:**Input a: 4

Input b: -2

Input c: -10

Output root1: 1.85

Output root2: -1.35

### Required knowledge

Basic C programming, Switch caseWe have already talked about what is Quadratic equation and how to find all roots of quadratic equation using if else. Here I will be explaining how to find roots of quadratic equation using switch case.

### Program to find roots of quadratic equation using switch case

/** * C program to find all roots of a quadratic equation using switch case */ #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); discriminant = (b*b) - (4*a*c); /* * Computes 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\n", 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\n", 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\n", root1, root2); break; } } return 0; }

**Note:**This method is not as optimum and feasible as using if else was. Hence it is recommended to use if else to find roots of quadratic equations.

Output

Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 4 -2 -10

Two distinct and real roots exists: 1.85 and -1.35

Two distinct and real roots exists: 1.85 and -1.35

Happy coding ;)

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).