C Program to find the roots of quadratic equation
Quadratic equations are the polynomial equation with degree 2. It is represented as ax2 + bx +c = 0, where a, b and c are the coefficient variable of the equation. The universal rule of quadratic equation defines that the value of 'a' cannot be zero, and the value of x is used to find the roots of the quadratic equation (a, b). A quadratic equation's roots are defined in three ways: real and distinct, real and equal, and real and imaginary.
Nature of the roots
The nature of the roots depends on the Discriminant (D) where D is.
- If D > 0, the roots are real and distinct (unequal)
- If D = 0, the roots are real and equal.
- If D < 0, the roots are real and imaginary.
Steps to find the square roots of the quadratic equation
- Initialize all the variables used in the quadratic equation.
- Take inputs of all coefficient variables x, y and z from the user.
- And then, find the discriminant of the quadratic equation using the formula:
Discriminant = (y * y) - (4 * x *z).
- Calculate the roots based on the nature of the discriminant of the quadratic equation.
- If discriminant > 0, then
Root1 = (-y + sqrt(det)) / (2 * x)
Root2 = (-y + sqrt(det)) / (2 * x)
Print the roots are real and distinct.
- Else if (discriminant = 0) then,
Root1 = Root2 = -y / (2 * x).
Print both roots are real and equal.
- Else (discriminant < 0), the roots are distinct complex where,
Real part of the root is: Root1 = Root2 = -y / (2 * x) or real = -y / (2 * x).
Imaginary part of the root is: sqrt( -discriminant) / (2 * x).
Print both roots are imaginary, where first root is (r + i) img and second root is (r - i) img.
- Exit or terminate the program.
Pseudo Code of the Quadratic Equation
- Input the coefficient variable, x, y and z.
- D <- sqrt (y * y - 4 * x * z).
- R1 <- (-y + D) / ( 2 * x).
- R2 <- (-y - D) / (2 * x).
- Print the roots R1 and R2.
Let's implements the above steps in a C program to find the roots of the quadratic equation.
Let's create another C program in which we have used function.