Problem 7 • Math
Author: Er. Manoj Kumar • Computer Science Fundamentals
Find Real & Complex Roots of a Quadratic Equation
📋
Problem Statement
Given coefficients a, b, and c of ax^2 + bx + c = 0, find both roots by evaluating the discriminant D = b^2 - 4ac across all three cases: D > 0 (two distinct real roots), D = 0 (equal real roots), and D < 0 (complex conjugate pairs).
📊
Variable Dictionary / Table
| Variable Name | Data Type | Role & Purpose |
|---|---|---|
| a, b, c | Float / Double | Quadratic coefficients (a != 0) |
| D | Float / Double | Discriminant value: (b^2 - 4ac) |
| root1, root2 | Float / Double | Real roots or components of complex roots |
📝
Step-by-Step Formal Algorithm
Step 1: Start.
Step 2: Read coefficients a, b, c.
Step 3: If a == 0:
Print 'Not quadratic (Linear equation)' and Stop.
Step 4: Compute Discriminant D = (b * b) - (4 * a * c).
Step 5: If D > 0 (Two distinct real roots):
root1 = (-b + sqrt(D)) / (2 * a)
root2 = (-b - sqrt(D)) / (2 * a)
Print root1, root2.
Step 6: Else if D == 0 (Two equal real roots):
root1 = -b / (2 * a)
Print 'Real and Equal Roots:', root1.
Step 7: Else (D < 0, Complex conjugate roots):
realPart = -b / (2 * a)
imagPart = sqrt(-D) / (2 * a)
Print 'Complex Roots: realPart +/- imagPart * i'.
Step 8: Stop.
🔀
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/"Read a, b, c"/]
In --> ChkA{"Is a == 0 ?"}
ChkA -- Yes --> Err[/"Print 'Not Quadratic'"/]
Err --> Stop([Stop])
ChkA -- No --> CalcD["D = b*b - 4*a*c"]
CalcD --> ChkD{"Is D > 0 ?"}
ChkD -- Yes --> OutReal[/"Print 2 Distinct Real Roots"/]
OutReal --> Stop
ChkD -- No --> ChkZero{"Is D == 0 ?"}
ChkZero -- Yes --> OutEq[/"Print Real & Equal Root"/]
OutEq --> Stop
ChkZero -- No --> OutComp[/"Print Complex Roots (real ± imag·i)"/]
OutComp --> Stop
🖥️
Flowchart (ANSI Terminal ASCII Chart)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read a, b, c |
+-----------------+
|
v
/ \
/ a == 0?\ ---YES--> [Print Not Quadratic] --> [STOP]
\ /
| NO
v
+-----------------+
| D = b*b - 4*a*c |
+-----------------+
|
v
/ \
/ D > 0? \ ---YES--> [Distinct Real Roots] --> [STOP]
\ /
| NO
v
/ \
/ D == 0?\ ---YES--> [Equal Real Roots] --> [STOP]
\ /
| NO
v
+-----------------------+
| Complex Roots (with i)| --> [STOP]
+-----------------------+
🧪
Dry-Run Execution Trace Table
| Execution Step / Case | Variables State 1 | Variables State 2 | Condition Evaluation | Output / Result |
|---|---|---|---|---|
| Case D > 0 | a=1, b=-5, c=6 | D = 25 - 24 = 1 | r1 = (5+1)/2 = 3, r2 = (5-1)/2 = 2 | Roots: 3.0 and 2.0 |
| Case D = 0 | a=1, b=-4, c=4 | D = 16 - 16 = 0 | r1 = r2 = 4/2 = 2 | Roots: Real & Equal = 2.0 |
| Case D < 0 | a=1, b=2, c=5 | D = 4 - 20 = -16 | real = -1, imag = sqrt(16)/2 = 2 | Roots: -1.0 ± 2.0i |
⚡
Interactive C++ Compiler & Runner
Live In-BrowserEdit the code directly with the Edit Code button, provide custom inputs, and press Run Code to compile and execute in the live terminal console below!
📄
problem_7.cpp
GCC 13.2 • C++20
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double a, b, c;
cout << "Enter coefficients a, b, and c (ax^2 + bx + c = 0): ";
if (!(cin >> a >> b >> c)) {
cout << "Invalid input! Please enter numeric coefficients.\n";
return 1;
}
if (a == 0) {
cout << "\n[!] Error: Coefficient 'a' cannot be 0 in a quadratic equation.\n";
if (b != 0) {
cout << "Linear root (bx + c = 0): x = " << (-c / b) << "\n";
}
return 0;
}
double D = (b * b) - (4.0 * a * c);
cout << fixed << setprecision(4);
cout << "\n========================================\n";
cout << "Equation : (" << a << ")x^2 + (" << b << ")x + (" << c << ") = 0\n";
cout << "Discriminant D: " << D << "\n";
cout << "----------------------------------------\n";
if (D > 0) {
double sqrtD = sqrt(D);
double root1 = (-b + sqrtD) / (2.0 * a);
double root2 = (-b - sqrtD) / (2.0 * a);
cout << "Nature: REAL, UNEQUAL, AND DISTINCT ROOTS\n";
cout << "Root 1 = " << root1 << "\n";
cout << "Root 2 = " << root2 << "\n";
} else if (D == 0) {
double root = -b / (2.0 * a);
cout << "Nature: REAL AND EQUAL ROOTS\n";
cout << "Root 1 = Root 2 = " << root << "\n";
} else {
double realPart = -b / (2.0 * a);
double imagPart = sqrt(-D) / (2.0 * a);
cout << "Nature: COMPLEX CONJUGATE ROOTS\n";
cout << "Root 1 = " << realPart << " + " << imagPart << "i\n";
cout << "Root 2 = " << realPart << " - " << imagPart << "i\n";
}
cout << "========================================\n";
return 0;
}
⌨️ Standard Input (cin):
Presets:
💻 Terminal Output
• Ready to run
Input: 1 2 5 Output: ======================================== Equation : (1.0000)x^2 + (2.0000)x + (5.0000) = 0 Discriminant D: -16.0000 ---------------------------------------- Nature: COMPLEX CONJUGATE ROOTS Root 1 = -1.0000 + 2.0000i Root 2 = -1.0000 - 2.0000i ========================================
Developer & Exam Key Takeaway
Handling D < 0 prevents mathematical domain errors such as std::sqrt(-16) from producing runtime NaN crashes.