Pages

Wednesday, November 11, 2015

C++ Program to Find All The Roots Of A Quadratic Equation


A quadratic equation has the form ax^2+bx+c=0 where x is an unknown, and a, b, c represent values which in this c++ program will be supplied by the user.



Also Read: C++ Program To compute least common multiple

#include<iostream.h>
#include<conio.h>
#include<math.h>


void main()
{
float a,b,c,d,x1,x2;

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

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

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

cout<<x1<<" "<<x2;
}