Write a C program to find all roots of a quadratic equation. How to find all roots of a quadratic equation using if else in C programming. Finding all roots of a quadratic equation in C programming.

Input a: 8

Input b: -4

Input c: -2

Output root1: 0.80

Output root2: -0.30

If the discriminant is positive then there are two real distinct roots given by:

If the discriminant is zero then it will have exactly one real root given by:

If the discriminant is negative then it will have two distinct complex roots given by:

Happy coding ;)

**Example:**Input a: 8

Input b: -4

Input c: -2

Output root1: 0.80

Output root2: -0.30

### Required knowledge

Basic C programming, If else#### Quadratic equation

As wikipedia states in elementary algebra a quadratic equation is an equation in the form of#### How to solve 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 byIf the discriminant is positive then there are two real distinct roots given by:

If the discriminant is zero then it will have exactly one real root given by:

If the discriminant is negative then it will have two distinct complex roots given by:

### Program to find roots of quadratic equation

/** * 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); discriminant = (b*b) - (4*a*c); /* * Computes roots of quadratic equation based on 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\n", root1, root2); } else if(discriminant == 0) { root1 = root2 = -b / (2*a); printf("Two equal and real roots exists: %.2f and %.2f\n", 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\n", root1, imaginary, root2, imaginary); } return 0; }

Output

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

Two distinct and real roots exists: 0.81 and -0.31

Happy coding ;)

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