/* Calculation of roots to a quadratic equation --------------------------------
ID: 3539349 • Letter: #
Question
/* Calculation of roots to a quadratic equation -------------------------------------------- Write a C++ program that prints all solutions to the quadratic equation ax^2 + bx + c = 0.
Your program should interactively read in the values for the quadratic equation parameters a, b, c and then use the quadratic formula to determine the roots.
Three test Cases: ------------------ In your screenshot(s) show the following test cases,
1. no real solutions -------------------- take the formula for a parabola y = x^2 and push it up, for example y = x^2 + 1. The graph of such a parabola does not cross the x-axis, so there are no real solutions. x^2 + 0*x + 1 = 0, has no real solutions
2. two distinct real solutions ------------------------------ x^2 + 0x - 1 = 0, has two distinct roots, namely 1 and -1 3. one solution -------------- x^2 - 2x + 1 = 0, has one distinct real root, namely at 1.
Just a beginners code nothing fancy.
Explanation / Answer
#include<iostream>
using namespace std;
int main() {
float a, b, c, root1, root2, delta;
cout << "Enter values for a, b & c in ax^2+bx+c: " << endl;
cin >> a >> b >> c;
if (a == 0)
cout << " Value of a should not be zero " << endl;
else {
delta = (b * b) - (4 * a*c);
if (delta > 0) {
root1 = (-b + sqrt(delta)) / (2 * a);
root2 = (-b - sqrt(delta)) / (2 * a);
cout << "Roots are real and unequal ";
cout << "Root1= " << root1;
cout << ", Root2= " << root2 << " ";
} else if (delta == 0) {
root1 = -b / (2 * a);
cout << "Roots are real and equal ";
cout << "Root1= " << root1;
cout << ", Root2= " << root1<<" ";
} else
cout << "Roots are complex and imaginary ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.