C program to 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. Finding all roots of a quadratic equation using switch case. Logic to calculate roots of quadratic equation in C program.

Example
Input

Input a: 4
Input b: -2
Input c: -10

Output

Root1: 1.85
Root2: -1.35

Required knowledge

Basic C programming, Switch case, Basic Mathematics

Read more - Program to find roots of quadratic equation using if else

Quadratic equation

In elementary algebra quadratic equation is an equation in the form of

Quadratic equation

Solving quadratic equation

A quadratic equation can have either one or two distinct real or complex roots depending upon nature of discriminant of the equation. Where discriminant of the quadratic equation is given by
Discriminant of a quadratic equation

Depending upon the nature of the discriminant, formula for finding roots can be given as:

  • Case 1: If discriminant is positive. Then there are two real distinct roots given by.
    Quadratic equation formula root 1
  • Case 2: If discriminant is zero. Then it have exactly one real root given by.
    Quadratic equation formula root 2
  • Case 3: If discriminant is negative. Then it will have two distinct complex roots given by.
    Quadratic equation formula root 3

Logic to find roots of quadratic equation

Below is the step by step descriptive logic to find roots of quadratic equation using switch case.

  1. Input coefficients of quadratic equation. Store it in some variable say a, b and c.
  2. Find discriminant of given equation using formula i.e. discriminant = (b*b) - (4*a*c).
  3. Compute the roots based on the nature of discriminant.
  4. Switch the value of discriminant > 0. Now here is the real game, discriminant > 0 will return either 1(true) or 0(false). Hence, for this switch there must only be two case 1(true) other for 0(false).
  5. For case 1 means discriminant is positive. Apply the formula to compute root1 and root2.
  6. For case 0 there exists two more case. Which is if the discriminant is not positive then it is either be negative or zero. Hence, inside case 0 switch the expression discriminant < 0. This switch can be called as nested switch.
  7. For the above switch there again will be two cases. Which is case 1 and case 0. Case 1 means discriminant is negative. Whereas case 0 means discriminant is zero.
  8. Apply the formula to compute roots again for both the inner cases.

Let us now code the solution for the program.

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);

    // 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;
}

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

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>