# C Programming (Turbo / Visual C++ Compiler) - Quadratic Equation Solver - ax2 + bx + c = 0

I have given here a C program to solve any Quadratic Equation. Quadratic equation is a second order of polynomial equation in a single variable.

x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a

We have to find the value of (b*b - 4*a*c).

1. When it is greater than Zero, we will get two Real Solutions.
2. When it is equal to zero, we will get one Real Solution.
3. When it is less than Zero, we will get two Imaginary Solutions.

When there is an imaginary solutions, we have to use the factor i to represent imaginary part as it is a complex number.

## Source Code

#include <math.h>

#include <stdio.h>

// quadratic equation is a second order of polynomial equation in a single variable

// x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a

void SolveQuadratic(double a, double b, double c)

{

double sqrtpart = b*b - 4*a*c;

double x, x1, x2, img;

if(sqrtpart > 0)

{

x1 = (-b + sqrt(sqrtpart)) / (2 * a);

x2 = (-b - sqrt(sqrtpart)) / (2 * a);

printf("Two Real Solutions: %.4lf or %.4lf\n\n", x1, x2);

}

else if(sqrtpart < 0)

{

sqrtpart = -sqrtpart;

x = -b / (2 * a);

img = sqrt(sqrtpart) / (2 * a);

printf("Two Imaginary Solutions: %.4lf + %.4lf i or %.4lf + %.4lf i\n\n", x, img, x, img);

}

else

{

x = (-b + sqrt(sqrtpart)) / (2 * a);

printf("One Real Solution: %.4lf\n\n", x);

}

}

int main()

{

// 6x^2 + 11x - 35 = 0

// 5x^2 + 6x + 1 = 0

// 2x^2 + 4x + 2 = 0

// 5x^2 + 2x + 1 = 0

return 0;

}

## Output

Two Real Solutions: 1.6667 or -3.5000

Two Real Solutions: -0.2000 or -1.0000

One Real Solution: -1.0000

Two Imaginary Solutions: -0.2000 + 0.4000 i or -0.2000 + 0.4000 i

Press any key to continue . . .