Introduction
A quadratic equation is an equation of the form ax²+bx+c=0, where a, b, and c are constants. In this lab, we will learn how to write a program to find the roots of a quadratic equation.
To solve the quadratic equation, we will use the following formula:
x = (-b ± sqrt(b² - 4ac)) / 2a
Note: You need to create the file
~/project/main.cyourself to practice coding and learn how to compile and run it using gcc.
cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main
Get Input Coefficients
We will take input from the user for coefficients of the quadratic equation such as a, b, and c. These coefficients will be used to calculate the roots of the quadratic equation.
float a, b, c, determinant, r1, r2, real, imag;
printf("\nEnter coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);
Calculate the Determinant
We will then calculate the value of the determinant of the quadratic equation using the formula:
determinant = b*b - 4*a*c;
determinant = b*b - 4*a*c;
Check the Nature of Roots
We will then check the nature of the roots of the quadratic equation based on the value of determinant calculated in step 2. If the determinant is greater than 0, then the roots are real and distinct. If the determinant is equal to 0, then the roots are real and equal. If the determinant is less than 0, then the roots are complex and imaginary.
if(determinant > 0) // both roots are real
{
r1 = (-b + sqrt(determinant))/2*a; // Brackets are important
r2 = (-b - sqrt(determinant))/2*a;
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
else if(determinant == 0) // both roots are real and equal
{
r1 = r2 = -b/(2*a); // brackets are important
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
/*
Determinant < 0 - both roots are imaginary of the
form real + i*imaginary
*/
else
{
real = -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag);
}
Output the Roots
Finally, we will output the roots of the quadratic equation based on the nature of the roots calculated in Step 3.
Write the Full Code
#include<stdio.h>
#include<math.h> // This is needed to use sqrt() function
int main()
{
float a, b, c, determinant, r1, r2, real, imag;
printf("\nEnter coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);
determinant = b*b - 4*a*c;
if(determinant > 0) // both roots are real
{
r1 = (-b + sqrt(determinant))/2*a; // Brackets are important
r2 = (-b - sqrt(determinant))/2*a;
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
else if(determinant == 0) // both roots are real and equal
{
r1 = r2 = -b/(2*a); // brackets are important
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
/*
Determinant < 0 - both roots are imaginary of the
form real + i*imaginary
*/
else
{
real = -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Summary
In this lab, we learned how to write a C program to find the roots of a quadratic equation. We took input from the user for coefficients of the quadratic equation and calculated the roots based on the nature of the roots determined by the value of determinant. We then output the roots of the quadratic equation based on the nature of the roots.



