Author Topic: Quadratic Equation  (Read 2947 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Quadratic Equation
« on: November 27, 2005, 10:53:07 AM »
Problem:

Write an equation to solve quadratic equations of the form
        Ax + Bx + C = 0

where the coefficients A, B and C are real numbers.  The two real number solutions are derived by the formula
__________ _________
______-B / B - 4AC
x = ---------------------
__________2A

For this exercise you may assume that A≠0 and the relationship
      B ≥ 4AC
holds so there will be a real-number solutions to x.

SOLUTION:

 
Code: [Select]

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    cout << "-- A PROGRAM TO SOLVE A QUADRATIC EQUATION --\n\n";
    char ans;    
    double A, B, C, x1, x2;
    do
    {
        cout << "Enter three real numbers for A, B, and C  (space delimited): ";
        cin >> A >> B >> C;
       
        x1 = ( -B + sqrt( pow(B,2.0)-4*A*C ) )/(2 * A);
        x2 = ( -B - sqrt( pow(B,2.0)-4*A*C ) )/(2 * A);
        cout << "\nSOLUTION : " << x1 << ", " << x2 << endl;
   
    cout << "\nRun again? [y/n] ";
    cin >> ans;
    } while ( ans == 'y' || ans == 'Y' );
   
    system("PAUSE");
    return 0;
}


Highlights:
Multiple input values using cin
Use of the do-while loop for offering the choice to run the program again
Use of some math functions
Analyze. Design. Develop. Debug. Deploy. Maintain.