C++ problem using quadratics

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
---------------------------------------------------------------

My solution so far

#include <iostream.h>
#include <math.h>
#include <conio.h>


void main()
{ int a, b, c;
clrscr();
cout << "Enter the 3 values of A, B, C \n ";
cin >> a >> b >> c;

if((b^2-4*a*c) < 0)
cout << "Discriminant (b^2 - 4*a*c) = Quadratic equation has no real solutions \n";
else if ((b^2-4*a*c) == 0)
cout << "Discriminant (b^2 - 4ac) = Quadratic equation has two coincided real solutions \n" << "X1 = " << (-b/2*a*c) <<"\n" << "X2 = " << (-b/2*a*c) <<"\n";
else if ((b^2-4*a*c) > 0)

cout << "Discriminant (b^2 - 4ac) = The two real solutions are: \n" << "X1 = " << (-b + sqrt(b^2 - 4*a*c))/2*a <<"\n" << "X2 = " << (-b+sqrt(b^2 - 4*a*c))/2*a <<"\n";
getch();


}

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.

Hi

Can anyone help with this above problem?.

Compiler used is Borlands C++.

I am having problems with the results it dislays if you run the program and look at the answers they dont match what they should be for example if the discriminant is less than zero it should print a value for x1 and x2 but it does not.
Last edited on
Where exactly in your code are you having problems?
Topic archived. No new replies allowed.