Difference between revisions of "Directory:Derek Elder/Programs/Quadratic Formula"

MyWikiBiz, Author Your Legacy — Friday November 08, 2024
Jump to navigationJump to search
(start of page)
 
(Modifications)
 
Line 11: Line 11:
 
int main()
 
int main()
 
{
 
{
 
 
int A = 0;
 
int A = 0;
 
int B = 0;
 
int B = 0;
Line 24: Line 23:
 
 
 
double discrim = sqrt((B * B) - 4 * A * C);
 
double discrim = sqrt((B * B) - 4 * A * C);
double imaginary_discrim = sqrt(-((B * B) - 4 * A * C));
+
double imaginary_discrim = sqrt(-((B * B) - (4 * A * C)));
  
if (discrim >= 0)
+
if(discrim >= 0)
 
{
 
{
 
cout<<"The value of the discriminant is: "<<discrim<<"\n";
 
cout<<"The value of the discriminant is: "<<discrim<<"\n";
Line 37: Line 36:
 
if(A != 0)
 
if(A != 0)
 
{
 
{
if(A <0)
+
if(A < 0)
 
{
 
{
 
A *= -1;
 
A *= -1;
Line 45: Line 44:
 
{
 
{
 
cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the two real solutions are:"<<"\n";
 
cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the two real solutions are:"<<"\n";
cout<<"X = "<<(-B + discrim)/(2.0 * A)<<"\n";
+
cout<<"X = "<<((-B + discrim)/(2.0 * A))<<"\n";
cout<<"X = "<<(-B - discrim)/(2.0 * A)<<"\n";
+
cout<<"X = "<<((-B - discrim)/(2.0 * A))<<"\n";
 
}
 
}
 
else if (discrim == 0)
 
else if (discrim == 0)
 
{
 
{
 
cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the one real solution is:"<<"\n";
 
cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the one real solution is:"<<"\n";
cout<<"X = "<<-B /(2.0 * A)<<"\n";
+
cout<<"X = "<<(-B/(2.0 * A))<<"\n";
 
}
 
}
 
else
 
else
 
{
 
{
 
cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the two imaginary solutions are:"<<"\n";
 
cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the two imaginary solutions are:"<<"\n";
cout<<"X = "<<-B / (2.0 * A)<<" + "<<imaginary_discrim /(2 * A)<<"i"<<"\n";  
+
cout<<"X = "<<(-B/(2.0 * A))<<" + "<<(imaginary_discrim/(2 * A))<<"i"<<"\n";  
cout<<"X = "<<-B / (2.0 * A)<<" - "<<imaginary_discrim /(2 * A)<<"i"<<"\n";  
+
cout<<"X = "<<(-B/(2.0 * A))<<" - "<<(imaginary_discrim/(2 * A))<<"i"<<"\n";  
 
}
 
}
  

Latest revision as of 17:32, 6 May 2008

#include <iostream>
#include <cmath>
using namespace std;

double sqrt(int discrim)
{
	return sqrt(double(discrim));
}

int main()
{
	int A = 0;
	int B = 0;
	int C = 0;

	cout<<"Please enter a value for A: ";
	cin>>A;
	cout<<"Please enter a value for B: ";
	cin>>B;
	cout<<"Please enter a value for C: ";
	cin>>C;
	
	double discrim = sqrt((B * B) - 4 * A * C);
	double imaginary_discrim = sqrt(-((B * B) - (4 * A * C)));

	if(discrim >= 0)
	{
		cout<<"The value of the discriminant is: "<<discrim<<"\n";
	}
	else
	{
		cout<<"The value of the discriminant is: "<<imaginary_discrim<<"i"<<"\n";
	}

	if(A != 0)
	{
		if(A < 0)
		{
			A *= -1;
		}

		if (discrim > 0)
		{
			cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the two real solutions are:"<<"\n";
			cout<<"X = "<<((-B + discrim)/(2.0 * A))<<"\n";
			cout<<"X = "<<((-B - discrim)/(2.0 * A))<<"\n";
		}
		else if (discrim == 0)
		{
			cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the one real solution is:"<<"\n";
			cout<<"X = "<<(-B/(2.0 * A))<<"\n";
		}
		else
		{
			cout<<"When A = "<<A<<", B = "<<B<<", and C = "<<C<<", the two imaginary solutions are:"<<"\n";
			cout<<"X = "<<(-B/(2.0 * A))<<" + "<<(imaginary_discrim/(2 * A))<<"i"<<"\n"; 
			cout<<"X = "<<(-B/(2.0 * A))<<" - "<<(imaginary_discrim/(2 * A))<<"i"<<"\n"; 
		}

	}
	else
		cout<<"There is no solution for the equation when A = 0."<<"\n";

	return 0;
}