### Author Topic: Quadratic Equation  (Read 3955 times)

#### louiecerv

• Moderator
• User
•     • Posts: 85 « 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.