SOURCE FILE: quadrat_v2.cpp
/*
This is a program to solve a quadratic equation like 5X^2 + 6X + 1 = 0.
*/
#include <iostream>
#include <cmath>
using namespace std;
int main (void)
{
double x1, x2 ;
double discr ;
double sqrtDiscr ;
double a, b, c ;
cout << endl ; // Start this set of outputs with a blank line
cout << "Please enter a real number for coefficient a: " ;
cin >> a ;
cout << "Please enter a real number for coefficient b: " ;
cin >> b ;
cout << "Please enter a real number for coefficient c: " ;
cin >> c ;
discr = b*b - 4*a*c ;
cout << endl ; // Start this set of outputs with a blank line
if ( discr < 0 )
{ // Do this if discr < 0
cout << "Sorry, no real solution.\n" ;
}
else
{ // Do this if discr >= 0
if (discr == 0)
{
// Do this if discr == 0
cout << "There is only one root: " << ( -b/(2*a) ) << endl ;
}
else
{
// Do this if discr > 0
sqrtDiscr = sqrt(b*b - 4*a*c) ;
x1 = (-b + sqrtDiscr) / (2*a) ;
x2 = (-b - sqrtDiscr) / (2*a) ;
cout << "The first root is: " << x1 << endl ;
cout << "The second root is: " << x2 << endl ;
}
}
cout << endl ; // Print a blank line at the end of everything
return 0 ;
}