Dice Roll Simulator code
#include
#include // For sqrt()
using namespace std;
int main() {
double a, b, c;
cout << "Enter coefficients a, b, and c: ";
cin >> a >> b >> c;
if (a == 0) {
cout << "This is not a quadratic equation." << endl;
return 1;
}
double discriminant = b*b - 4*a*c;
if (discriminant > 0) {
// Two real and distinct roots
double root1 = (-b + sqrt(discriminant)) / (2*a);
double 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) {
// One real root (roots are equal)
double root = -b / (2*a);
cout << "Roots are real and same." << endl;
cout << "Root = " << root << endl;
}
else {
// Complex roots
double realPart = -b / (2*a);
double imagPart = sqrt(-discriminant) / (2*a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + " << imagPart << "i" << endl;
cout << "Root 2 = " << realPart << " - " << imagPart << "i" << endl;
}
return 0;
}
Code output
Enter coefficients a, b, and c: 1 -5 6
Roots are real and different.
Root 1 = 3
Root 2 = 2