I don't understand this question fully could someone check the code that I have written to see if it does what the question asks me to do I will now give you the question then my code thanks.
Question:Exercise 5: Two roots of the quadratic equation is calculated by using the formula:
Where a, b and c are real values. The term is called discriminant. Write a program in C++ for the solutions of the quadratic equation for which . The program will calculate the discriminant first. Depending on the value of the discriminant it will do the following:
if the discriminant , it will display the message as follows
-----------------------------------------------------
Discriminant (b^2 – 4*a*c) =
Quadratic equation has no real solutions
-----------------------------------------------------
if the discriminant , it will display the solutions as follows
---------------------------------------------------------------
Discriminant (b^2 – 4ac) =
Quadratic equation has two coincided real solutions
X1=X2= (-b/2a) =
---------------------------------------------------------------
If the discriminant , it will display two solutions as follows:
--------------------------------------------------
Discriminant (b^2 – 4ac) =
The two real solutions are:
X1= (-b+sqrt(b^2 – 4ac)/2a =
X2= (-b-sqrt(b^2 – 4ac)/2a =
--------------------------------------------------
The program has to run at least 3 times to show the three situations.
My code:
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
|
#include <stdio.h>
#include <iostream>
#include <math.h>
// Program made by Mark Wylie
using namespace std;
int main()
{
int a,b,c;
double discriminant;
float root1, root2;
cout<<" enter the values of a"<< endl;
cin >> a;
cout<<" enter the values of b"<< endl;
cin >> b;
cout<<" enter the values of c"<< endl;
cin >> c;
discriminant = ((b^2)-(4*a*c));
root1 = ( -b + sqrt( discriminant)) / 2*a ;
root2 = ( -b - sqrt( discriminant)) / 2*a ;
if (discriminant < 0)
cout<< "Discriminant (b^2 – 4*a*c) = Quadratic equation has no real solutions"<< endl;
if (discriminant == 0)
cout << "Discriminant (b^2 – 4ac) = Quadratic equation has two coincided real solutionsX1=X2= (-b/2a) ="<< endl;
if (discriminant > 0)
cout << "Discriminant (b^2 – 4ac) = The two real solutions are:X1= (-b+sqrt(b^2 – 4ac)/2a = X2= (-b-sqrt(b^2 – 4ac)/2a ="<< endl;
cin >> root1;
cin >> root2;
system("pause");
return 0;
}
|