Quadratic equations are one of the most common types of equations that you will encounter in mathematics and science. They have the form: ax^2 + bx + c = 0 where a, b and c are constants and a is not equal to 0.
To find the values of x that satisfy the equation, we can use a formula called Bhaskara's formula, which is: x = {-b \pm \sqrt{b^2 - 4ac}}{2a}
This formula gives us two possible solutions for x, called the roots of the equation. However, sometimes there may be no real roots or only one real root, depending on the value of the expression under the square root, called the discriminant d = b^2 - 4ac
If d < 0 then there are no real roots, because we cannot take the square root of a negative number. If d = 0, then there is only one real root, because the plus and minus signs give the same value. If d > 0, then there are two distinct real roots.
In this program, we will show you how to write a C program that takes the coefficients a, b and c as input from the user and prints the roots of the quadratic equation using Bhaskara's formula. We will also display a message if it is not possible to find the roots.
C Program
Here is a possible C program to print the roots of Bhaskara’s formula from the given three floating numbers. Display a message if it is not possible to find the roots.
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c; // coefficients of the quadratic equation
float d; // discriminant
float r1, r2; // roots
printf("Enter the coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
if (a == 0) // not a quadratic equation
{
printf("Invalid input. a cannot be zero.\n");
return 0;
}
d = b * b - 4 * a * c; // calculate the discriminant
if (d < 0) // no real roots
{
printf("No real roots exist.\n");
}
else if (d == 0) // one real root
{
r1 = -b / (2 * a); // calculate the root
printf("The root is: %.2f\n", r1);
}
else // two real roots
{
r1 = (-b + sqrt(d)) / (2 * a); // calculate the first root
r2 = (-b - sqrt(d)) / (2 * a); // calculate the second root
printf("The roots are: %.2f and %.2f\n", r1, r2);
}
return 0;
}
Explanation
1. The first step is to include the necessary header files. We need stdio.h for input and output functions, and math.h for the square root function.
#include <stdio.h>
#include <math.h>
2. Next, we declare some variables to store the coefficients and the roots of the equation. We use float data type to allow decimal values. We also declare a variable to store the discriminant.
float a, b, c; // coefficients of the quadratic equation
float d; // discriminant
float r1, r2; // roots
3. Then, we prompt the user to enter the coefficients and read them using scanf function.
printf("Enter the coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
4. We also check if a is zero, because in that case the equation is not quadratic and Bhaskara's formula does not apply. We print an error message and terminate the program if that happens.
if (a == 0) // not a quadratic equation
{
printf("Invalid input. a cannot be zero.\n");
return 0;
}
5. next, we calculate the discriminant using the formula:
d = b * b - 4 * a * c; // calculate the discriminant
6. Then, we use if-else statements to check the value of the discriminant and print the roots accordingly. If d < 0, we print that no real roots exist. If d == 0, we print the single root using the formula:
x = \frac{-b}{2a}
If d > 0, we print both roots using Bhaskara's formula:
x = \frac{-b \pm \sqrt{d}}{2a}
7. We use %.2f format specifier to print two decimal places for each root.
if (d < 0) // no real roots
{
printf("No real roots exist.\n");
}
else if (d == 0) // one real root
{
r1 = -b / (2 * a); // calculate the root
printf("The root is: %.2f\n", r1);
}
else // two real roots
{
r1 = (-b + sqrt(d)) / (2 * a); // calculate the first root
r2 = (-b - sqrt(d)) / (2 * a); // calculate the second root
printf("The roots are: %.2f and %.2f\n", r1, r2);
}
8. Finally, we return 0 to indicate successful execution of the program.
return 0;
Output
Here is an example of how our program works. Suppose we enter 1, -5 and 6 as the coefficients. Then our program prints:
Enter the coefficients a, b and c: 1 -5 6
The roots are: 3.00 and 2.00
This means that the equation x^2 - 5x + 6 = 0 has two real roots: x = 3 and x = 2.
Here is another example. Suppose we enter 1, -4 and 4 as the coefficients. Then our program prints:
Enter the coefficients a, b and c: 1 -4 4
The root is: 2.00
This means that the equation x^2 - 4x + 4 = 0 has one real root: x = 2.
Here is a final example. Suppose we enter 1, 2 and 3 as the coefficients. Then our program prints:
Enter the coefficients a, b and c: 1 2 3
No real roots exist.
This means that the equation x^2 + 2x + 3 = 0 has no real roots, because the discriminant is negative.
Conclusion
In this program, we have learned how to write a C program that solves quadratic equations using Bhaskara's formula. We have also learned how to handle different cases depending on the value of the discriminant. We hope you found this tutorial helpful and informative. If you have any questions or feedback, please leave a comment below. Thank you for reading!
We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!