finding the roots of the quadratic equation using switch-statement



#include<stdio.h>
#include<conio.h>
void main()
{
int option;
float a,b,c,d;
float root1,root2,r1,r2;
clrscr();
printf("enterthe value of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
if (a==0||b==0||c==0)
{
printf("\n error_we cannot analyse");
exit();
}
d=(b*b-4*a*c);

if(d>0)
 option=1;
else

 if(d<0)
 option=2;
else
 option=3;
switch(option)
{
case1:
printf("roots are real & distinct\n");
root1=(-b+sqrt(d))/2*a;
root2=(-b-sqrt(d))/2*a;
printf("\n root1=%f root2=%f\n",root1,root2);
break;

case 2:
printf("\n roots are imaginary & complex\n");
r1=-b/2*a;
r2=sqrt(abs(d))/2*a;
printf("root1=%f+%f\n",r1,r2);

case 3:
printf("\n roots are real & equal\n");
root1=root2=-b/2*a;
printf("root1=%f\n,root2=%f\n",root1,root2);
break;
}
getch();
}



OUTPUT

enter the value of a,b,c
22,33,44
 roots are imaginary & complex
root1=-363.000000+27962.000000

roots are real & equal
root2=-363.000000

Related Posts Plugin for WordPress, Blogger...