C program to find root of a quadratic equation; Through this tutorial, you will learn how to find the root of a quadratic equation in c program.

The standard form of a quadratic equation is:

ax^{2}+ bx + c = 0, where a, b and c are real numbers and a != 0

The term `b`

is known as the ^{2;} - 4ac**discriminant** of a quadratic equation. It tells the nature of the roots.

- If the discriminant is greater than
`0`

, the roots are real and different. - If the discriminant is equal to
`0`

, the roots are real and equal. - If the discriminant is less than
`0`

, the roots are complex and different.

## Programs To Find Roots of a Quadratic Equation

- Algorithm to find roots of a quadratic equation
- C Program to Find Roots of a Quadratic Equation using if else
- C Program to Find Roots of a Quadratic Equation using switch case

### Algorithm to find roots of a quadratic equation

Use the following steps to write a c program to find the roots of a quadratic equation ax2 + bx + c = 0; as follows:

- Start Program
- Read the value of a, b, c.
- Calculate k = b*b – 4*a*c.
- If (d < 0) Display “Roots are Imaginary, calculater1 = (-b +i ? k)/ 2a and r2 =(b + i? k)/ 2a. else if (d = 0) Display “Roots are Equal” and calculate r1 = r2 = (-b / 2*a) …
- Print r1 and r2.
- End Program

### C Program to Find Roots of a Quadratic Equation using if else

```
/**
* C program to find all roots of a quadratic equation
*/
#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);
/* Find discriminant of the equation */
discriminant = (b * b) - (4 * a * c);
/* Find the nature of discriminant */
if(discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2);
}
else if(discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
}
else if(discriminant < 0)
{
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);
}
return 0;
}
```

The output of the above c program; as follows:

Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 8 -4 -2 Two distinct and real roots exists: 0.81 and -0.31

### C Program to Find Roots of a Quadratic Equation using switch case

```
#include <stdio.h>
#include<math.h>
int main()
{
float a, b, c;
float root1, root2, imaginary, discriminant;
printf("\n Please Enter values of a, b, c of Quadratic Equation : ");
scanf("%f%f%f", &a, &b, &c);
discriminant = (b * b) - (4 * a *c);
switch(discriminant > 0)
{
case 1:
root1 = (-b + sqrt(discriminant) / (2 * a));
root2 = (-b - sqrt(discriminant) / (2 * a));
printf("\n Two Distinct Real Roots Exists: root1 = %.2f and root2 = %.2f", root1, root2);
break;
case 0:
switch(discriminant < 0)
{
case 1: //True
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("\n Two Distinct Complex Roots Exists: root1 = %.2f+%.2f and root2 = %.2f-%.2f", root1, imaginary, root2, imaginary);
break;
case 0: // False (Discriminant = 0)
root1 = root2 = -b / (2 * a);
printf("\n Two Equal and Real Roots Exists: root1 = %.2f and root2 = %.2f", root1, root2);
break;
}
}
return 0;
}
```

The output of the above c program; as follows:

Please Enter values of a, b, c of Quadratic Equation : 2 3 5 Two Distinct Complex Roots Exists: root1 = -0.75+1.39 and root2 = -0.75-1.39