# C Program to find the Roots of a Quadratic Equation

### What is Quadratic Equation:

Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as **ax ^{2} + bx + c = 0**. The value of "a" cannot be zero, according to the quadratic equation's universal rule, and the value of "x" is what's used to determine the quadratic equation's roots (a, b). Real and distinct, real and equal, and real and imaginary roots are three different methods to describe a quadratic equation's roots.

### Quadratic Equation:

```
ax2 + bx + c = 0
(x = (-b ± √ (b2 - 4ac)) /(2a)
```

**Characteristics of Roots:**

- If
**Discriminant> 0**, the roots are real and not equal. - If
**Discriminant = 0**, the roots are real and equal. - If
**Discriminant < 0**, the roots are real and imaginary.

### How to Solve the Quadratic Equation's Square Roots:

- Considering some variables which will be used to solve quadratic equation problem.
- Variables should be initialized.
- The user will put three values as input,
- Those values will be stored in the variables.
- Then we have to find discriminant(d) of the quadratic equation.

Formula:**d = j * j - 4 * i * k**; - If (d > 0), then
**rt1 = (-j + sqrt(d)) / (2 * i);****rt2 = (-j - sqrt(d)) / (2 * i);**

Roots of quadratic Equation are real and not equal. - Else if (d==0), then
**rt1 = rt2 = -j / (2 * i)**

Roots of quadratic Equation are real and equal. - Else (d < 0), then
**real_num = -j / (2 * i);****img_num = sqrt(-d) / (2 * i)**

Roots of quadratic equation are imaginary. - Exit

**C Program to calculate the roots of a quadratic equation:**

```
//Code
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
//considering variables
float i, j, k, d, rt1, rt2, real_num, img_num;
//asks user for three inputs
printf("\n Enter the values of the coefficients i, j and k: \n ");
//storing values in the variables
scanf("%f %f %f", &i, &j, &k);
//formula to find the discriminant(d) of the quadratic equation
d = j * j - 4 * i * k;
// defines the conditions that the roots of the quadratic equation are real and not same
if (d > 0)
{
rt1 = (-j + sqrt(d)) / (2 * i);
rt2 = (-j - sqrt(d)) / (2 * i);
printf("\n Root 1 value = %.2f and Root 2 value = %.2f", rt1, rt2);
}
// elseif condition defines both roots real are equal in the quadratic equation
else if (d == 0)
{
rt1 = rt2 = -j / (2 * i); // both roots are equal;
printf("\nRoot 1 value = %.2f and Root 2 value = %.2f", rt1, rt2);
}
//else condition defines both the roots are imaginary
else {
real_num = -j / (2 * i);
img_num = sqrt(-d) / (2 * i);
printf("\nRoot 1 value = %.2f + %.2fi and Root 2 value = %.2f - %.2fi ", real_num, img_num, real_num, img_num);
}
return 0;
}
```

**Example 1:**

If the user puts 3, 4, 5 as inputs;

**Output:**

```
Enter the values of the coefficients i, j and k:
3
4
5
Root 1 value = -0.67 + 1.11i and Root 2 value = -0.67 - 1.11i
```

**Example 2:**

If the user puts 1, -2, 1 as inputs;

**Output:**

```
Enter the values of the coefficients i, j and k:
1
-2
1
Root 1 value = 1.00 and Root 2 value = 1.00
```

**Example 3:**

If the user puts 1, -5, 6 as inputs;

**Output:**

```
Enter the values of the coefficients i, j and k:
1
-5
6
Root 1 value = 3.00 and Root 2 value = 2.00
```

**Example 4:**

If the user puts 2, -9, -5 as inputs;

**Output:**

```
Enter the values of the coefficients i, j and k:
2
-9
-5
Root 1 value = 5.00 and Root 2 value = -0.50
```

**Time Complexity:**

Let us consider, d = discriminant.

Time complexity will be **O ( log (d) )**

**Space Complexity:**

Space complexity will be **O ( 1 )**