Write a program to find the nature of the roots as
well as value of the roots. However, in case of imaginary roots , find the real
part & imaginary part separately.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c, r1, r2, d;
cout<<"Enter the coefficients:- ";
cin>>a>>b>>c;
if(a==0)
cout<<"Aborting..";
else
{
d=b*b-4*a*c;
if(d>=0)
{
cout<<"Roots are real ";
if(d>0)
cout<<"and distinct and the values are ";
else
cout<<"and equal and the values are ";
r1=(-b+sqrt(d))/2*a;
r2=(-b-sqrt(d))/2*a;
cout<<r1<<" and "<<r2;
}
else
{
cout<<"Roots are unreal and the values are ";
r1=-b/2*a;
r2=sqrt(-d)/2*a;
cout<<r1<<" +i"<<r2<<" and
"<<r2<<" -i"<<r2;
getch();
}
}
}
Output
your program is not working. I execute it in Online IDE https://www.codechef.com/ide
ReplyDelete