Sunday, 8 May 2011

write c program to find the roots of a quadratic equation

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

void main()

{

float a,b,c,x1,x2,disc;

clrscr();

printf("Enter the co-efficients\n");

scanf("%f%f%f",&a,&b,&c);

disc=b*b-4*a*c;/*to find discriminant*/

if(disc>0)/*distinct roots*/

{

x1=(-b+sqrt(disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

printf("The roots are distinct\n");

exit(0);

}

if(disc==0)/*Equal roots*/

{

x1=x2=-b/(2*a);

printf("The roots are equal\n");

printf("x1=%f\nx2=%f\n",x1,x2);

exit(0);

}

x1=-b/(2*a);/*complex roots*/

x2=sqrt(fabs(disc))/(2*a);

printf("The roots are complex\n");

printf("The first root=%f+i%f\n",x1,x2);

printf("The second root=%f-i%f\n",x1,x2);

getch();

}

No comments:

Post a Comment