1. Program to find the roots of a quadratic equation.
#include <iostream.h>
#include <math.h>
#include <conio.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imagPart;
cout << "Enter coefficients a, b, and c: ";
cin >> a >> b >> c;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << root1 << endl;
cout << "Root 2 = " << root2 << endl;
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
cout << "Roots are real and same." << endl;
cout << "Root 1 = Root 2 = " << root1 << endl;
} else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + i" << imagPart << endl;
cout << "Root 2 = " << realPart << " - i" << imagPart << endl;
}
getch();
return 0;
}
1. Program to find the roots of a quadratic equation.
#include <iostream.h>
#include <math.h>
#include <conio.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imagPart;
cout << "Enter coefficients a, b, and c: ";
cin >> a >> b >> c;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << root1 << endl;
cout << "Root 2 = " << root2 << endl;
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
cout << "Roots are real and same." << endl;
cout << "Root 1 = Root 2 = " << root1 << endl;
} else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + i" << imagPart << endl;
cout << "Root 2 = " << realPart << " - i" << imagPart << endl;
}
getch();
return 0;
}