A quadratic equation is a second-degree polynomial equation in the form ax² + bx + c = 0, where a
, b
, and c
are constants and a
is not zero. Quadratic equations are common in mathematics, physics, and engineering, and solving them programmatically in C is a great way to practice conditional statements, math functions, and input/output operations.
The roots of a quadratic equation can be real or complex and are calculated using the quadratic formula:
x = (-b ± √(b² – 4ac)) / (2a)
Here, the expression b² – 4ac is called the discriminant. It determines the type of roots: if it is positive, the roots are real and distinct; if zero, the roots are real and equal; if negative, the roots are complex.
C Program to Solve a Quadratic Equation
Below is a complete C program that calculates and displays the roots of a quadratic equation.
#include <stdio.h> // For input/output functions like printf and scanf
#include <math.h> // For mathematical functions like sqrt
int main() {
// Declare variables for coefficients, discriminant, and roots
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
// Ask the user to enter the coefficients of the quadratic equation
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
// Check if 'a' is zero, because a quadratic equation requires a != 0
if(a == 0) {
puts("a cannot be zero."); // Print error message
return 0; // Exit the program
}
// Calculate the discriminant of the quadratic equation: b^2 - 4ac
discriminant = b * b - 4 * a * c;
// Case 1: discriminant > 0 -> roots are real and different
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a); // First root
root2 = (-b - sqrt(discriminant)) / (2 * a); // Second root
printf("Roots are real and different.\n");
printf("Root 1 = %.2lf\n", root1);
printf("Root 2 = %.2lf\n", root2);
// Case 2: discriminant == 0 -> roots are real and the same
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a); // Both roots are equal
printf("Roots are real and the same.\n");
printf("Root = %.2lf\n", root1);
// Case 3: discriminant < 0 -> roots are complex and different
} else {
realPart = -b / (2 * a); // Real part of the complex root
imaginaryPart = sqrt(-discriminant) / (2 * a); // Imaginary part
printf("Roots are complex and different.\n");
printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);
printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);
}
return 0; // End of program
}
In this program, we first declare variables to store the coefficients a
, b
, c
, the discriminant, and the roots. We ask the user to enter the coefficients using scanf()
. The discriminant is calculated as b² – 4ac.
If the discriminant is greater than zero, the roots are real and different. We calculate each root using (-b ± √discriminant) / (2a) and display them. If the discriminant is zero, both roots are equal, so we calculate one root and print it.
If the discriminant is negative, the roots are complex. We calculate the real part as -b / (2a) and the imaginary part as √(-discriminant) / (2a). The program then prints the complex roots in the form real ± imaginary i
. Using math.h
allows us to calculate the square root with sqrt()
.
Common Beginner Mistakes
When solving quadratic equations in C, beginners often make these mistakes:
- Not checking if
a
is zero. Ifa
is zero, the equation is not quadratic, and dividing by zero will cause an error. - Using
sqrt()
on a negative number without handling complex roots, which leads to incorrect or undefined results.
To avoid these issues, always check that a
is non-zero and verify the discriminant before calculating roots to ensure accurate results.
FAQs
Q1: Can this program solve equations with fractional coefficients?
Yes, using double
variables allows the program to handle decimal coefficients accurately.
Q2: How can I display more decimal places for roots?
You can change %.2lf
in printf()
to %.4lf
or more to display additional decimal places.
Q3: Can this program handle all quadratic equations, including those with complex roots?
Yes, the program checks the discriminant and handles both real and complex roots automatically.
Conclusion
Solving a quadratic equation in C helps beginners understand conditional statements, mathematical functions, and input/output operations. By implementing this program, you can calculate roots for any quadratic equation, handle complex numbers, and practice logical thinking in programming. This knowledge also forms the foundation for more advanced math-based C programs.
References & Additional Resources
- Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
- GeeksforGeeks: C Program for Quadratic Equation Roots – Example and explanation.
- Programiz: C Math Functions – Learn about
sqrt()
and other math functions. - TutorialsPoint: C Input/Output – Basics of
scanf()
andprintf()
in C.