quadratic equation

Write a program that solves quadratic equations.

A quadratic equation is an equation of the form ax2 + bx + c =0, where a, b, and c are given coefficients and x is the unknown. The solutions for the quadratic function are given by,

x=(-b±√(b^2-4ac))/2a
Some conditions that you must check in your program are:
i) b2-4ac must be a positive value
ii) value a must not be 0.
Use the sqrt() function to compute the square root. Sample runs of the program are:

Run 1:
Please enter the coefficients of the quadratic equation: 2 1 -6
The equation is: 2x^2 + 1x – 6 = 0
The solutions are: 1.5, -2.

Run 2:
Please enter the coefficients of the quadratic equation: 0 2 3
Error: Unable to compute the solution.



solution:

#include<iostream>
#include<cmath>
using namespace std;
int main()

{
float a,b,c;

cout<<"Enter value a:";
cin>>a;
cout<<"Enter value b:";
cin>>b;
cout<<"Enter value c:";
cin>>c;

float differential = (pow(b,b)-4*a*c);
float root_positif = (((-b)+sqrt(differential))/(2*a));
float root_negatif = (((-b)-sqrt(differential))/(2*a));

if(differential==0)
{
cout<<"The differential is "<<endl;
cout<<differential<<endl;
cout<<"The equation are single root."<<endl;
}
else if(differential<0)
{
cout<<"The differential is"<<endl;
cout<<differential<<endl;
cout<<"The equation are two complex root."<<endl;
}
else
{
cout<<"The differential is"<<endl;
cout<<differential<<endl;
cout<<"The equation are two real root."<<endl;
}

can anyone check whether i do it correctly or not?
i think you need to check this


1
2
3
float differential = (pow(b,2)-4*a*c);
float root_positif = (((-b)+sqrt(differential))/(2*a)); //need to check what it will behave if divide by zero
float root_negatif = (((-b)-sqrt(differential))/(2*a)); //need to check what it will behave if divide by zero 
float differential = (pow(b,b)-4*a*c); should be float differential = (pow(b,2)-4*a*c); because pow(b,b) == bb

http://www.cplusplus.com/reference/clibrary/cmath/pow/
why i put a=0 still can compile n run? but the sample run there cannot run,error happen...
This code complies for me

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
int _tmain(int argc, _TCHAR* argv[])
{
	float a,b,c;

		cout<<"Enter value a:";
		cin>>a;
		cout<<"Enter value b:";
		cin>>b;
		cout<<"Enter value c:";
		cin>>c;

		float differential = (pow(b,b)-4*a*c);
		float root_positif = (	(	(-b)+sqrt(differential))	/(2*a));
		float root_negatif = (	(	(-b)-sqrt(differential))	/(2*a));

		if(differential==0)
		{
			cout<<"The differential is "<<endl;
			cout<<differential<<endl;
			cout<<"The equation are single root."<<endl;
		}
		else if(differential<0)
		{
			cout<<"The differential is"<<endl;
			cout<<differential<<endl;
			cout<<"The equation are two complex root."<<endl;
		}
		else
		{
			cout<<"The differential is"<<endl;
			cout<<differential<<endl;
			cout<<"The equation are two real root."<<endl;
		}
	return 0;
}
Like "bluecoder" has pointed out already, you should check to see if a == 0, before computing root_positif and root_negatif. The program may compile okay, but if you execute it, and a == 0, you will get a "divide by zero" error.

I also suggest that you check to see if differential >= 0 right after it is computed.
If it is negative, when you compute root_positif and root_negatif with the sqrt function, you'll get an error. You'll have to deal with it if it is negative.

Another suggestion:
instead of repeating the two lines outputting differential, take them out of the if conditions. That way you are not repeating those two lines three times:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout<<"The differential is "<<endl;
cout<<differential<<endl;

if(differential==0)
{
  cout<<"The equation are single root."<<endl;
}
else if(differential<0)
{
  cout<<"The equation are two complex root."<<endl;
}
else
{
  cout<<"The equation are two real root."<<endl;
}


It wouldn't be too hard to make the program handle complex results too. Will you be adding this capability in the future?

FYI: there are many quadratic equation solvers available online, if you would like to check the results of your program against the results of another program, for example,

Quadratic Equation Solver
http://www.akiti.ca/Quad2Deg.html

Hope this helps.
Topic archived. No new replies allowed.