1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
#include <cmath>
#include <math.h>
#include <stdio.h>
using namespace std;
int main()
{{
int a , b , c;
float xi , x;
char response , Y , N;
do {
cout << "Equation format is ax^2 + bx +c" << endl;
cout << "Input value of a\n" , cin >> a;
cout << "Input value of b\n" , cin >> b;
cout << "Input value of c\n" , cin >> c;
if ( a == 1 ) {
cout << "x^2 + " << b << "x + " << c << " = 0" << endl;
}
else cout << a << "x^2 + " << b << "x + " << c << " = 0" << endl;
cout << "Is this correct, (Y/N)?\n" , cin >> response;
}
while ( response != 'Y' );
x = b * b - 4 * a * c;
if ( x == 0 ) {
x = - b / (2 * a);
cout << "x = " << x << endl;
return 0;
}
else if ( x > 0 ) {
x = (- b - pow( ( b * b ) - 4 * a * c , (0.5)))/ ( 2 * a );
cout << "x = " << x << endl;
cout << "or" << endl;
x = ( - b + pow( ( b * b ) - 4 * a * c , (0.5)))/ ( 2 * a );
cout << "x = " << x << endl;
return 0;
}
else if ( x < 0 )
x = ( - b / 2 * a );
xi = ( pow(( 4 * a * c - ( b * b ) ) , (0.5))) / ( 2 * a );
if ( xi == 1 ){
cout << "x = " << x << " + " << "i" << endl;
cout << "or" << endl;
cout << "x = " << x << " - " << "i" << endl;
}
else {
cout << "x = " << x << " + " << xi << "i" << endl;
cout << "or" << endl;
cout << "x = " << x << " - " << xi << "i" << endl;
}}
return 0;
}
|