May 9, 2013

C++ program to find the roots of a quadratic equation.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
float a,b,c,root1,root2,delta;
char choice;
do
{
clrscr();
cout<<"ENTER THE VALUES FOR THE VARIABLESIN EQUATION : ax^2 + bx + c :";
cout<<"\nENTER a : ";
cin>>a;
cout<<"\nENTER b : ";
cin>>b;
cout<<"\nENTER c : ";
cin>>c;
if(!a)
{
cout<<"\nINVALID ENTRYFOR a.";
exit(0);
}
delta=(b*b)-(4*a*c);
if(delta<0)
cout<<"\nIMAGINARY ANDCOMPLEX ROOTS.";
else if (delta>0)
{
root1=(-b+sqrt(delta))/(2*a);
root2=(-b-sqrt(delta))/(2*a);
cout<<"\nROOTS ARE REAL AND UNEQUAL."<<endl;
cout<<"\nROOTS ARE : "<< root1 << "\t" <<root2;
}
else if(delta==0)
{
root1=(-b/(2*a));
root2=root1;
cout<<"\nROOTS ARE REAL AND EQUAL."<<endl;
cout<<"\nROOTS ARE : "<< root1 << "\t" <<root2;
}
cout<<"\n\nDO YOU WANT TO CONTINUE (Y/N) : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}

OUTPUT

ENTER THE VALUES FOR THE VARIABLESIN EQUATION : ax^2 + bx + c :
ENTER a : 3
ENTER b : 5
ENTER c : 2
ROOTS ARE REAL AND UNEQUAL.
ROOTS ARE : -0.666667 -1
DO YOU WANT TO CONTINUE (Y/N) : n

0 comments:

Post a Comment