(C++) CPP Programs - 2
Friday, December 14, 2007
2. Write a C program to find and output all the roots of a given Quadratic equation for non zero co-efficient. Incase of errors, your program should report suitable messages.
/* Quadratic equation*/
#include
#include
#include
#include
void main()
{
float a,b,c,root1,root2,root,ip,rp,disc;
clrscr();
printf("Enter the co-efficients of the quadratic equation\n\a");
scanf("%f%f%f",&a,&b,&c);
if(a*b*c==0)
{
printf("Equation is not possible\n\a");
exit(0);
getch();
}
printf("Equation is possible\n\a");
disc=(b*b)-(4*a*c);
if(disc==0)
{
printf("Roots are real and equal\n\a");
root=-b/(2*a);
printf("Roots are %f and %f\n\a",root,root);
}
else if(disc>0)
{
printf("Roots are real\n\a");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Roots are %f and%f\n\a",root1,root2);
}
else
{
printf("Roots are complex\n\a");
rp=-b/2*a;
ip=sqrt((fabs)(disc))/2*a;
printf("Real part=%f+i%f\nImaginary part=%f+i%f\n",rp,ip,rp,ip);
}
getch();
}
0 comments:
Post a Comment