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.
- 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.
- Start
- 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.
- Stop
Steps to find the square roots of the quadratic equation
Pseudo Code of the Quadratic Equation
Let's incorporate the aforementioned instructions in a C program to determine the solutions of the quadratic equation.
Example
/* C Program to Find the Roots of the Quadratic Equation */
#include<stdio.h>
#include<math.h> // it is used for math calculation
#include<conio.h>
void main()
{
float x, y, z, det, root1, root2, real, img;
printf("\n Enter the value of coefficient x, y and z: \n ");
scanf("%f %f %f", &x, &y, &z);
// define the quadratic formula of the nature of the root
det = y * y - 4 * x * z;
// defines the conditions for real and different roots of the quadratic equation
if (det > 0)
{
root1 = (-y + sqrt(det)) / (2 * x);
root2 = (-y + sqrt(det)) / (2 * x);
printf("\n Value of root1 = %.2f and value of root2 = %.2f", root1, root2);
}
// elseif condition defines both roots ( real and equal root) are equal in the quadratic equation
else if (det == 0)
{
root1 = root2 = -y / (2 * x); // both roots are equal;
printf("\n Value of root1 = %.2f and Value of root2 = %.2f", root1, root2);
}
// if det < 0, means both roots are real and imaginary in the quadratic equation.
else {
real = -y / (2 * x);
img = sqrt(-det) / (2 * x);
printf("\n value of root1 = %.2f + %.2fi and value of root2 = %.2f - %.2fi ", real, img, real, img);
}
getch();
}
Output:
Output
Enter the value of coefficient x, y and z:
Value of root1 = %.2f and value of root2 = %.2f
Value of root1 = %.2f and Value of root2 = %.2f
value of root1 = %.2f + %.2fi and value of root2 = %.2f - %.2fi
Let's develop a new C program that incorporates a function.
Example
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
// use function to check the nature of the roots in the quadratic equation
void R_Quadratic( int x, int y, int z)
{
if (x == 0) // Checks the vakue of x, if x = 0, the equation is not quadratic equation.
{
printf(" The value of x cannot be zero");
return;
}
int det = y * y - 4 * x * z;
double sqrt_det = sqrt(abs (det));
if (det > 0)
{
printf("\n Both roots are real and different \n ");
printf("%.2f\n %.2f", (double) (-y + sqrt_det) / (2 * x), (double) (-y - sqrt_det) / (2 * x));
}
else if (det == 0)
{
printf("\n Both roots are real and same ");
printf("%.2f", -(double)y / (2 * x));
}
else
{
printf("\n Both roots are complex");
printf("\n %.2f + %.2fi \n%.2f - %.2fi", -(double)y / (2 * x), sqrt_det, -(double)y / (2 * x), sqrt_det);
}
}
void main()
{
int x, y, z; // declare variables x, y and z
printf("\n Enter the value of coefficient x, y and z: ");
scanf("%d %d %d", &x, &y, &z);
R_Quadratic(x, y, z); // call function R_Quadratic()
getch();
}
Output: