# C program to find all roots of a quadratic equation

Explore a C program to find all roots of a quadratic equation efficiently. Learn how to handle real, equal, and complex roots using the quadratic formula.

Quadratic equations play a fundamental role in mathematics and science, representing a key concept in algebra. In this article, we'll explore how to create a simple C program to find all roots of a quadratic equation.

### Understanding Quadratic Equations

A quadratic equation is of the form `ax^2 + bx + c = 0`

, where `a`

, `b`

, and `c`

are constants and `x`

represents the variable. The roots of a quadratic equation are the values of `x`

that satisfy the equation.

## C program to find all roots of a quadratic equation

Let's delve into the C programming language to create a program that calculates all roots of a quadratic equation using the quadratic formula.

```
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
float discriminant, root1, root2, realPart, imaginaryPart;
// Input from the user
printf("Enter coefficients (a, b, c): ");
scanf("%f %f %f", &a, &b, &c);
// Calculating discriminant
discriminant = b * b - 4 * a * c;
// Checking conditions to find roots
if (discriminant > 0) {
// Real and different roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different: %.2f and %.2f", root1, root2);
} else if (discriminant == 0) {
// Real and equal roots
root1 = root2 = -b / (2 * a);
printf("Roots are real and equal: %.2f and %.2f", root1, root2);
} else {
// Complex roots
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different: %.2f + %.2fi and %.2f - %.2fi", realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
```

**Output**

```
Enter coefficients (a, b, c): 1 -3 2
Roots are real and different: 2.00 and 1.00
Enter coefficients (a, b, c): 1 2 1
Roots are real and equal: -1.00 and -1.00
Enter coefficients (a, b, c): 1 1 1
Roots are complex and different: -0.50 + 0.87i and -0.50 - 0.87i
```

### Understanding the Code

**User Input:**The user is prompted to enter coefficients (`a`

,`b`

, and`c`

) of the quadratic equation using`scanf()`

.**Calculating Discriminant:**The program calculates the discriminant (`b^2 - 4ac`

), which determines the nature of roots.**Finding Roots:**Based on the value of the discriminant, the program identifies and computes the roots using conditional statements.

In this article, we've created a simple C program that efficiently finds all roots of a quadratic equation using the quadratic formula. Understanding the discriminant and employing conditional checks through C programming allows us to compute real, equal, or complex roots accurately.