Write a program to find roots of a quadratic equation
A quadratic equation is a second order equation having a single variable. Any quadratic equation can be represented as , where a, b and c are constants ( a can't be 0) and x is unknown variable.
For Example
is a quadratic equation where a, b and c are 2, 5 and 3 respectively.
To calculate the roots of quadratic equation we can use below formula. There are two solutions of a quadratic equation.
x = (-b + sqrt(D))/(2*a)
x = (-b - sqrt(D))/(2*a)
where, D = (b*b-4*a*c) is Discriminant, which differentiate the nature of the roots of quadratic equation.
For the complex result:
realPart = -b/(2*a);
imaginaryPart =sqrt(-D)/(2*a);
Note: We have used sqrt()
function to find square root which is in math.h library.
Example input 1:
1 2 1
Example output 1:
Roots of 1.00x^2 + 2.00x + 1.00 = 0 are real and same
x1 = x2 = -1.00
Example input 2:
1 -3 2
Example output 2:
Roots of 1.00x^2 + -3.00x + 2.00 = 0 are real and different
x1 = 2.00
x2 = 1.00
Example input 3:
1 2 2
Example output 3:
Roots of 1.00x^2 + 2.00x + 2.00 = 0 are complex and different
x1 = -1.00+1.00i
x2 = -1.00-1.00i