Write Ac Program To Find Quadratic Equation
A quadratic equation is a polynomial equation of the second degree. It plays a fundamental role in various fields of science and engineering. In this article, you will learn how to write a C program to find the roots (solutions) of a quadratic equation using the standard quadratic formula.
Problem Statement
The general form of a quadratic equation is given by ax^2 + bx + c = 0, where a, b, and c are real numbers, and a is not equal to zero. The goal is to find the values of x that satisfy this equation, also known as its roots. The nature of these roots (real, complex, distinct, or equal) depends on the discriminant.
Example
Consider the quadratic equation x^2 - 5x + 6 = 0.
Here, a=1, b=-5, and c=6.
The roots of this equation are x = 2 and x = 3. These are real and distinct roots.
Background & Knowledge Prerequisites
To understand and implement the solution, readers should be familiar with:
- Basic C programming: Variables, data types (especially
floatordouble), input/output operations (printf,scanf). - Conditional statements:
if-else if-elsefor handling different scenarios. - Mathematical functions in C: Specifically, the
sqrt()function from themath.hlibrary for calculating square roots. - Floating-point arithmetic: Understanding how to work with decimal numbers.
Setup Information:
You will need a C compiler (like GCC) to compile and run the program. The math.h library is standard and usually linked automatically, but some compilers might require adding -lm flag during compilation (e.g., gcc program.c -o program -lm).
Use Cases or Case Studies
Quadratic equations are essential for modeling and solving problems in diverse areas:
- Physics: Calculating projectile motion (e.g., trajectory of a ball, flight path of a rocket), determining the time it takes for an object to fall under gravity.
- Engineering: Designing parabolic antennas, optimizing structural elements, analyzing electrical circuits.
- Finance: Modeling growth rates, calculating compound interest over specific periods.
- Geometry: Finding dimensions of shapes with known areas, determining intersection points of curves.
- Economics: Optimizing profit functions or cost functions in business models.
Solution Approaches
The most common and robust approach to finding the roots of a quadratic equation involves using the quadratic formula. The formula is:
x = [-b ± sqrt(b^2 - 4ac)] / 2a
The term (b^2 - 4ac) is called the discriminant, often denoted by Δ or D. The value of the discriminant determines the nature of the roots:
-
D > 0(Positive Discriminant): Two distinct real roots. -
D = 0(Zero Discriminant): One real root (or two equal real roots). -
D < 0(Negative Discriminant): Two complex conjugate roots.
Approach 1: Implementing the Quadratic Formula
This approach directly applies the quadratic formula, handling the three discriminant cases using conditional logic.
One-line Summary: Calculate the discriminant first to determine the nature of the roots, then apply the appropriate part of the quadratic formula.
Code Example:
// Find Roots of Quadratic Equation
#include <stdio.h>
#include <math.h> // Required for sqrt() function
int main() {
// Step 1: Declare variables for coefficients and roots
double a, b, c;
double discriminant, root1, root2, realPart, imagPart;
// Step 2: Prompt user for coefficients
printf("Enter coefficient a: ");
scanf("%lf", &a);
printf("Enter coefficient b: ");
scanf("%lf", &b);
printf("Enter coefficient c: ");
scanf("%lf", &c);
// Step 3: Check if 'a' is zero
if (a == 0) {
printf("Error: Coefficient 'a' cannot be zero for a quadratic equation.\\n");
printf("This is a linear equation: %.2lfX + %.2lf = 0\\n", b, c);
// For a linear equation, if b is also 0, it's either no solution or infinite solutions.
// Otherwise, x = -c/b
if (b != 0) {
printf("Root for linear equation: X = %.2lf\\n", -c/b);
} else {
if (c == 0) {
printf("Infinite solutions (0 = 0).\\n");
} else {
printf("No solution (e.g., 5 = 0).\\n");
}
}
return 1; // Exit with an error code
}
// Step 4: Calculate the discriminant
discriminant = b * b - 4 * a * c;
// Step 5: Determine roots based on the discriminant's value
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two distinct real roots: X1 = %.2lf and X2 = %.2lf\\n", root1, root2);
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a); // Both roots are equal
printf("One real root (or two equal real roots): X = %.2lf\\n", root1);
} else { // discriminant < 0
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a); // Use -discriminant because discriminant is negative
printf("Two complex conjugate roots: X1 = %.2lf+%.2lfi and X2 = %.2lf-%.2lfi\\n", realPart, imagPart, realPart, imagPart);
}
return 0; // Indicate successful execution
}
Sample Output 1 (Distinct Real Roots):
Enter coefficient a: 1
Enter coefficient b: -5
Enter coefficient c: 6
Two distinct real roots: X1 = 3.00 and X2 = 2.00
Sample Output 2 (Equal Real Roots):
Enter coefficient a: 1
Enter coefficient b: -4
Enter coefficient c: 4
One real root (or two equal real roots): X = 2.00
Sample Output 3 (Complex Conjugate Roots):
Enter coefficient a: 1
Enter coefficient b: 2
Enter coefficient c: 5
Two complex conjugate roots: X1 = -1.00+2.00i and X2 = -1.00-2.00i
Sample Output 4 (a=0):
Enter coefficient a: 0
Enter coefficient b: 2
Enter coefficient c: 4
Error: Coefficient 'a' cannot be zero for a quadratic equation.
This is a linear equation: 2.00X + 4.00 = 0
Root for linear equation: X = -2.00
Stepwise Explanation:
- Include Headers:
stdio.hfor input/output andmath.hfor thesqrt()function. - Declare Variables:
a, b, cto store coefficients,discriminantfor its calculated value, androot1, root2, realPart, imagPartto store the computed roots. Usingdoubleprovides better precision for calculations. - Get Input: The program prompts the user to enter the coefficients
a,b, andc. - Handle
a = 0: A quadratic equation requiresato be non-zero. Ifais 0, it becomes a linear equation, and the program provides a message and attempts to solve it as such, then exits. - Calculate Discriminant: The value of
b*b - 4*a*cis computed and stored in thediscriminantvariable. - Conditional Root Calculation:
- If
discriminant > 0: Two distinct real roots are calculated using(-b + sqrt(discriminant)) / (2*a)and(-b - sqrt(discriminant)) / (2*a). - If
discriminant == 0: One real root is calculated as-b / (2*a). Bothroot1androot2will be equal. - If
discriminant < 0: Two complex conjugate roots exist. The real part is-b / (2*a), and the imaginary part issqrt(-discriminant) / (2*a). Note thesqrt(-discriminant)because the discriminant itself is negative, andsqrt()expects a non-negative argument.
- Print Results: The calculated roots are displayed with appropriate messages indicating their nature.
- Return 0: Indicates successful program execution.
Conclusion
Finding the roots of a quadratic equation is a fundamental mathematical task with widespread applications. By understanding the quadratic formula and the role of the discriminant, we can create a robust C program that correctly handles all possible scenarios: two distinct real roots, one repeated real root, and two complex conjugate roots. This program demonstrates the practical application of conditional logic and mathematical functions in C programming.
Summary
- Quadratic equations are of the form
ax^2 + bx + c = 0, wherea ≠ 0. - The discriminant (
D = b^2 - 4ac) determines the nature of the roots. -
D > 0: Two distinct real roots. -
D = 0: One real root (or two equal real roots). -
D < 0: Two complex conjugate roots. - The C program uses
if-else if-elsestatements to apply the quadratic formula based on the discriminant's value. - It is crucial to include
math.hfor thesqrt()function and usedoublefor precision. - The program also handles the edge case where
ais zero, treating it as a linear equation.