# Roots Of A Quadratic Equation

## Finding Roots of a Quadratic Equation

In this C program, we will find the roots of a quadratic equation [ax2 + bx + c]. We can solve a Quadratic Equation by finding its roots. Mainly roots of the quadratic equation are represented by a parabola in 3 different patterns like :

• No Real Roots
• One Real Root
• Two Real Roots

We get the roots of the equation which satisfies any one of the above conditions :
X = [-b (+or-)[Squareroot(pow(b,2)-4ac)]]/2a

Sample Test Case

Enter value of a :1
Enter value of b :-7
Enter value of c :12
Output
Two Real Roots
4.0
3.0 ## Algorithm

1. Start.
2. Take input from user a,b,c.
3. Check the value of a i.e. a!=0.
4. Calculate Discriminant (D)
• D = b^2 – 4*a*c
5. If D>0 : Two real root exists.
6. If D=0 : Equal root exists.
7. If D<0 : Imaginary root exists.
8. Display the existence of roots and the roots of the equation.
9. End.

## C Code

`#include <math.h>`
#include <stdio.h>
int main() {
double abcdroot1root2ri;
printf(“Enter value of a, b and c: “);
scanf(“%lf %lf %lf”, &a, &b, &c);

d = b * b – 4 * a * c;

// condition for real and different roots
if (d > 0) {
printf(“Two Real Roots\n);
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b – sqrt(d)) / (2 * a);
printf(“root1 = %.2lf \nroot2 = %.2lf”root1root2);
}

// condition for real and equal roots
else if (d == 0) {
printf(“Equal Roots\n);
root1 = root2 = –b / (2 * a);
printf(“root1 = root2 = %.2lf;”root1);
}

// if roots are not real
else {
r = –b / (2 * a);
i = sqrt(-d) / (2 * a);
printf(“No Real Roots\n);
printf(“root1 = %.2lf+%.2lfi \nroot2 = %.2f-%.2fi”riri);
}

return 0;

## Output

Enter value of a, b and c: 1 –7 12

Two Real Roots
root1 = 4.00
root2 = 3.00