You can find quality tech news,tricks,study materials etc contact me:facebook.com/ashwin.sunilkumar
Support Me...........
Please support me by giving donation
contact: aswinavofficial@gmail.com
Total Pageviews
Monday, September 1, 2014
C++ program to find roots of a quadratic equation
// Program to find the roots of a quadratic equation
#include
#include
using namespace std;
int main() {
int a, b, c, d;
double x1, x2;
cout << "Program to find roots of quadratic eq" << endl;
cout << "Enter values for ax^2 + bx + c..." << endl;
cout << "Enter value for a: ";
cin >> a;
cout << "Enter value for b: ";
cin >> b;
cout << "Enter value for c: ";
cin >> c;
cout << endl;
if (a==0)
cout << "Not a quadratic equation." << endl;
else {
d = (b*b)-(4*a*c);
if (d > 0) {
cout << "Real and distinct roots" << endl;
x1=(-b + sqrt(d))/(2*a);
x2=(-b - sqrt(d))/(2*a);
cout << "Root 1 = " << x1 << endl;
cout << "Root 2 = " << x2 << endl;
}
else if (d == 0) {
cout << "Real and equal roots." << endl;
x1 = x2 = -b/(2*a);
cout << "Root 1 = " << x1 << endl;
cout << "Root 2 = " << x2 << endl;
}
else {
cout << "Imaginary Roots." << endl;
x1 = -b/(2*a);
x2 = sqrt(-d)/(2*a);
cout << "Root 1 = " << x1 << endl;
cout << "Root 2 = " << x2 << endl;
}
}
return 0;
}
// aiuto
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment