Skip to main content

quadratic-1.0

//quadratic-1.0.cpp
//Simple program to solve a quadratic equation.
//Reads coefficients from keyboard and outputs to screen.
//Cannot cope with imaginary solutions.
//Steven Bamford

#include <cstdlib>
#include <iostream.h>
#include <cmath>
using namespace std;

int main()
{
  double a, b, c;  //coefficients of quadratic 
                   //a*x*x + b*x + c = 0
  double des;      //variable for the discriminant
  double x1, x2;   //two solutions

  cout << "Enter coefficients a, b, and c:" << endl;
  cin >> a >> b >> c;  //read coeffs from keyboard
  
  des = b*b - 4*a*c;            //calc discriminant
  x1 = (-b + sqrt(des))/(2*a);  //solution 1
  x2 = (-b - sqrt(des))/(2*a);  //solution 2

  //output solutions to screen
  cout << "Solutions are: " << x1 << " and " << x2 << endl;

  system("PAUSE");
  return(0);
}