Directory:Derek Elder/Programs/Quadratic Formula

MyWikiBiz, Author Your Legacy — Sunday December 22, 2024
Jump to navigationJump to search
#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;
}