Q:

C program to find all roots of a quadratic equation using switch case

belongs to collection: C Programming on Numbers

0

In this exercise, we learn how to write a C program to find all roots of a quadratic equation using switch case?. We will write the C program to find all roots of a quadratic equation using switch case. Write C program to find roots of quadratic equations using switch statements. How to find all roots of a quadratic equation using if else in C programming. Logic to find roots of quadratic equation in C programming.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#include <math.h>
int main()
{
    float a, b, c;
    float root1, root2, imaginary, discriminant;
    printf("Enter value of 'a' of quadratic equation (aX^2 + bX + c): ");
    scanf("%f", &a);
    printf("Enter value of 'b' of quadratic equation (aX^2 + bX + c): ");
    scanf("%f",&b);
    printf("Enter values of 'c' of quadratic equation (aX^2 + bX + c): ");
    scanf("%f",&c);
    // Find discriminant of the equation
    discriminant = (b * b) - (4 * a * c);
    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 value of a of quadratic equation (aX^2 + bX + c): 2
Enter value of b of quadratic equation (aX^2 + bX + c): 7
Enter values of c of quadratic equation (aX^2 + bX + c): 2
Two distinct and real roots exist: -0.31 and -3.19

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find the roots of a quadratic equatio... >>
<< C Program to print the two digit number in words...