# 1. Introduction

In this post, we will explore a C program that finds the roots of a quadratic equation using functions.

# 2. Program Overview

Our program will:

1. Define a function to calculate the discriminant.

2. Define a function to compute the real and complex roots.

3. In the main function, receive the coefficients ( a, b,) and ( c ) from the user and utilize the above functions to display the roots.

# 3. Code Program

``````#include<stdio.h>
#include<math.h>

// Function to calculate the discriminant
double discriminant(double a, double b, double c) {
return (b * b) - (4 * a * c);
}

// Function to compute the roots
void computeRoots(double a, double b, double c, double *root1, double *root2) {
double disc = discriminant(a, b, c);

if (disc > 0) {
*root1 = (-b + sqrt(disc)) / (2 * a);
*root2 = (-b - sqrt(disc)) / (2 * a);
} else if (disc == 0) {
*root1 = *root2 = -b / (2 * a);
} else {
printf("Roots are complex.\n");
*root1 = *root2 = -b / (2 * a);
double imaginary = sqrt(-disc) / (2 * a);
printf("Root 1: %lf + %lf i\n", *root1, imaginary);
printf("Root 2: %lf - %lf i\n", *root1, imaginary);
}
}

int main() {
double a, b, c, root1, root2;

printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);

computeRoots(a, b, c, &root1, &root2);

if (discriminant(a, b, c) >= 0) {
printf("Root 1: %lf\n", root1);
printf("Root 2: %lf\n", root2);
}

return 0;
}
``````

### Output:

```For a=1, b=-5, and c=6, the program would output:
Root 1: 3.000000
Root 2: 2.000000
```

# 4. Step By Step Explanation

1. We first calculate the discriminant, which helps us determine the nature of the roots (real and different, real and same, or complex).

2. The computeRoots function computes and prints the roots based on the discriminant's value. For a positive discriminant, we have two distinct real roots. For a discriminant of zero, we have two identical real roots. For a negative discriminant, the roots are complex.

3. In the main function, we capture the input coefficients and then call the functions to display the roots to the user.