# C program to Find all Roots of a Quadratic equation

To understand this example, you should have knowledge of following C programming topics:

Visit this page to learn about roots of quadratic equation if you don't know how to compute it.

Suppose you want of find roots of a quadratic equation ax^{2}+bx+c=0 where a, b and c are coefficients. This program will ask the coefficients: a, b and c from user and displays the roots.

## Source Code to Find Roots of Quadratic Equation

```
/* C Program to find roots of a quadratic equation when coefficients are entered by user. */
/* Library function sqrt() computes the square root. */
#include <stdio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
```

**Output 1**

Enter coefficients a, b and c: 2.3 4 5.6 Roots are: -0.87+1.30i and -0.87-1.30i

**Output 2**

Enter coefficients a, b and c: 4 1 0 Roots are: 0.00 and -0.25

To solve this program, library function `sqrt()`

is used. This function calculates the square root of a number. Learn more about, sqrt() function.