/* Program for finding "Roots of a Quadratic Equation" and also "Type of Roots" after getting "a" (Co-efficient of x^2), "b" (Co-efficient of x) & "c" (Constant term) as input from user. */ #include <iostream> #include <cmath> using namespace std; int main() { float a, b, c, disc, r1, r2, sq; cout << "Input a,b & c?\t"; cin >> a >> b >> c; disc = b*b - (4 * a*c); if (disc > 0) cout << "\nRoots are Real" << endl; else if (disc < 0) cout << "\nRoots are Non-Real" << endl; else cout << "\nRoots are Real & Equal" << endl; if (disc == 0) { r1 = r2 = -b / 2 * a; cout << "Roots are\t" << r1 << " & " << r2 << endl; } else if (disc > 0) { sq = sqrt(disc); r1 = (-b - sq) / 2 * a; r2 = (-b + sq) / 2 * a; cout << "Roots are\t" << r1 << " & " << r2 << endl; } else { cout << "Roots are\t" << -b << "-sqrt(i" << -disc << ") / " << 2 * a << "\n"; cout << "Roots are\t" << -b << "+sqrt(i" << -disc << ") / " << 2 * a << "\n"; } return 0; }
This blog is about coding in some commonly used "High Level Languages" like C, C++, Java etc. At first, I will post here basic programs written in C++ language only.
Tuesday, November 25, 2014
C++ Program for finding "Roots of a Quadratic Equation"
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment