Quadratic Equation

I've been looking over this problem I have for my programming class, and quite honestly, I can't figure out how I should write this quadratic equation.

In case anyone reading this doesn't know, quadratic equation is :

x = (-b +- sqrt((b^2)-4ac))/2a

Now my question is, how would I put that into C++ in my program? (I haven't started it yet because I want to figure out how to write this equation into it first)

Thanks!
You must use two different variables for two different solutions:

#include <math.h> // for sqrt

x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 *a);

x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
Last edited on
That's what I thought it was, just wasn't sure

So all I would have to do is put in the loop code for the number of times I want it run and put in the appropriate cout and cin statments?

Thanks
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
#include <iostream.h>
#include <cmath>

int main ()

{
	//Declare Variables
	double x,x1,x2,a,b,c;

	cout << "Input values of a, b, and c." ;
	cin >>a >>b >>c;

	for (int i = 1; i <= 10; ++i);
	{
		if ((b * b - 4 * a * c) > 0)
		cout << "x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)" &&
		cout << "x2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)";

		if else ((b * b - 4 * a * c) = 0)
		cout << "x = ((-b + sqrt(b * b - 4 * a * c)) / (2 * a)"

		if else ((b * b - 4 * a * c) < 0)
			cout << "x1 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a) &&
			cout << "x2 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a);
	}

	return (0);

}


Well...that's what I can come up with so far...the handout from our teacher (I think) very vaguely explained how to code it if the discriminant (b*b - 4*a*c) < 0, so I just input it as I thought it would work.

If anyone could look over this and point out necessary adjustments I need to make, it would be much appreciated.

Well, I think I realized some stupid mistakes I made, but I'll just see what kind of input I get.

(Due tomorrow by 130 by the way)
Last edited on
code looks good. some syntax errors suggest you havent tried running it.

I have no idea if sqrt(-1) will work but yea u can try. I used to manually punch in the 'i's in the imaginary part for my old assignments.

alternatively, there are many complex maths headers that let u compute complex numbers with ease.
try to execute the following code
i hope it will work properly

#include<math.h>
#include<iostream.h>
#include<process.h>
int main()
{
int a,b,c,discriminant;
float r1,r2;
cout<<"enter the values of a,b,c";
cin>>a>>b>>c;
discriminant=((b*b)-4*a*c);
if(discriminant>0)
{
r1=(-1*b+sqrt(discriminant))/(2*a);
r2=(-1*b-sqrt(discriminant))/(2*a);
}
else if(discriminant==0)
{
r1=r2=(-1*b)/(2*a);
}
else
{
discriminant=-1*discriminant;
cout<<"r1="<<"("<<(-1*b)<<"+ i"<<sqrt(discriminant)<<")/"<<2*a;
cout<<"r1="<<"("<<(-1*b)<<"+ i"<<sqrt(discriminant)<<")/"<<2*a;
exit(0);
}
cout<<"r1="<<r1<<"\n";
cout<<"r2="<<r2<<"\n";
return 0;
}

Topic archived. No new replies allowed.