July 14, 2021, Learn eTutorial

879

**A Quadratic equation in algebra can be defined as any equation that can be written in the format ax^2+bx+c=0.** For a better understanding of this **finding Quadratic equation roots C program** example, we always recommend you to learn the basic topics of C programming listed below:

In algebra, a quadratic equation is an equation that can be rearranged in the form **'ax^2+bx+c=0 **', where **'x'** represents an unknown quantity, and **'a'**, **'b'**, and **'c'** are numbers that are not equal to **0**. If '**a = 0**', then it is not a quadratic equation, it's called a linear equation. The numbers **'a'**, **'b'**, and **'c'** are the coefficients of the equation and **'x'** is the unknown value in the equation that has to find out. In this C program, we need to solve the quadratic equation and find its roots of it.

C program to find all the roots of a quadratic equation for non-zero coefficients is a little lengthy program, but it is very simple if you know the logic behind the program. We are using the formula **"disc = b*b - 4.0*a*c** after finding the "**disc**" we can check the roots are real or imaginary depending on if the value of the disc is greater than or less than zero.

**Note:** Checking the quadratic equation, if '**a=0**' then it is not a quadratic equation. Now check **'A'** or **'B'** or **'C'** is zero. If so we cannot determine roots. Else apply the formula **"b*b - 4*a*c"**. Finally, the check result is greater than or less than zero or zero itself.

- If the disc is less than zero then the real roots can be found out by using the equation
**"B/(2.0*A)"**. and the imaginary root will find out by "**sqrt(abs(disc))/(2.0*A)**". - If the disc is zero then the roots are the same which can be found out by "
**-B/(2.0*A)**". - If the disc is greater than zero then the roots will be
**(-B+sqrt(disc))/(2.0*A)**and**(-B-sqrt(disc))/(2.0*A)**.

**STEP 1:** Import the needed header libraries to the C program to use the needed built-in functions.

**STEP 2:** Accept the values for **A** and **B** and **C** using `printf`

and `scanf`

built-in functions.

**STEP 3:** Check if the value of **A** is not equal to zero. if so it's not a quadratic equation.

**STEP 4:** Use '`else`

' to check the value of the '**disc**' using the formula '**B*B - 4.0*A*C**'.

**STEP 5:** If the **disc** is less than zero then it will be imaginary roots.

**STEP 6:** Find out the roots real and imaginary using the formula and print the roots using `printf`

.

**STEP 7:** Use an '`else if`

' statement to check the disc is equal to zero and then the roots will be equal and real.

**STEP 8:** Calculate the roots using a formula and print the roots using a print statement.

**STEP 9:** Use the '`else if`

' to check the disc is greater than zero. if so print the roots as real and distinct.

**STEP 10:** Calculate both roots using a specific formula and print the result using a `printf`

built-in function in C programming.

` ````
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main() {
float A, B, C, root1, root2;
float realp, imagp, disc;
printf("Enter the values of A, B and C\n");
scanf("%f %f %f", & A, & B, & C); /* If A = 0, it is not a quadratic equation */
if (A == 0 || B == 0 || C == 0) {
printf("Error: Roots cannot be determined\n");
exit(1);
} else {
disc = B * B - 4.0 * A * C;
if (disc < 0) {
printf("Imaginary Roots\n");
realp = -B / (2.0 * A);
imagp = sqrt(abs(disc)) / (2.0 * A);
printf("Root1 = %f +i %f\n", realp, imagp);
printf("Root2 = %f -i %f\n", realp, imagp);
} else if (disc == 0) {
printf("Roots are real and equal\n");
root1 = -B / (2.0 * A);
root2 = root1;
printf("Root1 = %f \n", root1);
printf("Root2 = %f \n", root2);
} else if (disc > 0) {
printf("Roots are real and distinct\n");
root1 = (-B + sqrt(disc)) / (2.0 * A);
root2 = (-B - sqrt(disc)) / (2.0 * A);
printf("Root1 = %f \n", root1);
printf("Root2 = %f \n", root2);
}
}
}
```

RUN 1 Enter the values of A, B and C 3 2 1 Imaginary Roots Root1 = -0.333333 +i 0.471405 Root2 = -0.333333 -i 0.471405 RUN 2 Enter the values of A, B and C 1 2 1 Roots are real and equal Root1 = -1.000000 Root2 = -1.000000 RUN 3 Enter the values of A, B and C 3 5 2 Roots are real and distinct Root1 = -0.666667 Root2 = -1.000000