0% found this document useful (0 votes)
2K views2 pages

C Program To Find All Roots of A Quadratic Equation Using Switch Case

This C program uses a switch case statement to determine the nature of the discriminant of a quadratic equation and calculate the corresponding roots. It takes in coefficients a, b, c, calculates the discriminant, and depending on if it is positive, negative or zero, uses the appropriate formula to find one or two real roots or a pair of complex roots, printing the result.

Uploaded by

rokeya sultana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views2 pages

C Program To Find All Roots of A Quadratic Equation Using Switch Case

This C program uses a switch case statement to determine the nature of the discriminant of a quadratic equation and calculate the corresponding roots. It takes in coefficients a, b, c, calculates the discriminant, and depending on if it is positive, negative or zero, uses the appropriate formula to find one or two real roots or a pair of complex roots, printing the result.

Uploaded by

rokeya sultana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

/**

* 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

You might also like