Introduction
A quadratic equation is a second-order polynomial equation in a single variable, commonly written as:
ax² + bx + c = 0
The values of ‘a’, ‘b’, and ‘c’ are constants, and ‘x’ represents the unknown variable. The solutions to the quadratic equation are obtained using the quadratic formula:
x = (-b ± √(b² – 4ac)) / 2a
This program will help you solve quadratic equations by calculating the roots of the equation based on the values of ‘a’, ‘b’, and ‘c’.
Objective
The objective of this program is to compute the roots of a quadratic equation. Depending on the discriminant (b² – 4ac), the program will return either two real roots, one real root, or no real roots. This will be handled using conditional statements and the quadratic formula.
Code
#include #include using namespace std; int main() { double a, b, c, discriminant, root1, root2; // Taking input from the user for the coefficients a, b, and c cout << "Enter the coefficients of the quadratic equation (a, b, c): "; cin >> a >> b >> c; // Checking if the equation is valid if (a == 0) { cout << "The value of 'a' cannot be zero for a quadratic equation." << endl; return 1; } // Calculating the discriminant discriminant = b * b - 4 * a * c; // Checking the nature of the roots if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); cout << "The equation has two real roots: " << root1 << " and " << root2 << endl; } else if (discriminant == 0) { root1 = -b / (2 * a); cout << "The equation has one real root: " << root1 << endl; } else { cout << "The equation has no real roots." << endl; } return 0; }
Program Structure and Explanation
The program follows a simple structure:
- Input: The user is prompted to enter the coefficients ‘a’, ‘b’, and ‘c’ for the quadratic equation.
- Validation: The program checks if the value of ‘a’ is zero. If ‘a’ is zero, the equation is not quadratic, and the program exits with a message.
- Discriminant Calculation: The discriminant (b² – 4ac) is calculated. The discriminant helps determine the nature of the roots.
- Root Calculation: Based on the value of the discriminant:
- If the discriminant is positive, two real roots are calculated.
- If the discriminant is zero, one real root is calculated.
- If the discriminant is negative, the program informs the user that there are no real roots.
How to Run the Program
To run this program, follow these steps:
- Open a C++ Integrated Development Environment (IDE) or a text editor (like Visual Studio, Code::Blocks, or any text editor) and create a new C++ file.
- Copy and paste the provided code into your C++ file.
- Save the file with a “.cpp” extension (e.g.,
quadratic_solver.cpp
). - Compile the program using your C++ compiler. For example, using the command:
g++ quadratic_solver.cpp -o quadratic_solver
- Run the compiled program using the command:
./quadratic_solver
- Input the coefficients when prompted, and the program will display the roots of the quadratic equation.