Write a program to find the roots of and quadratic equation of type ax2+bx+c where a is not equal to zero.

Source Code

#include<iostream>
#include<math.h>
using namespace std;


int main()
{
	float a,b,c,d,root1,root2;
	cout<<"Enter value of  a, b and c : ";
	cin>>a>>b>>c;

	d=b*b-4*a*c;

	if(d==0)
	{
		root1=(-b)/(2*a);
		root2=root1;
		cout<<"Roots are real & equal";
  	}
	else if(d>0)
	{
		root1=-(b+sqrt(d))/(2*a);
		root2=-(b-sqrt(d))/(2*a);
		cout<<"Roots are real & distinct";
	}
	else
	{
		root1=(-b)/(2*a);
		root2=sqrt(-d)/(2*a);
		cout<<"Roots are imaginary";
	}

	cout<<"\nRoot 1= "<<root1<<"\nRoot 2= "<<root2;


	return 0;
}


Output

SAMPLE RUN # 1

Enter value of a, b and c : 1.0 -10.0 25.0
Roots are real & equal
Root 1= 5
Root 2= 5

SAMPLE RUN # 2

Enter value of a, b and c : 1.0 5.0 2.0
Roots are real & distinct
Root 1= -4.56155
Root 2= -0.438447

SAMPLE RUN # 3

Enter value of a, b and c : 1.0 2.0 5.0
Roots are imaginary
Root 1= -1
Root 2= 2