Thursday, August 19, 2010

C Programming - calculate the roots of quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,e,x1,x2,d;
clrscr();
printf("\n enter the coefficient=");
scanf("\n %f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("\n imaginary root=");
}
if(d==0)
{
printf("\n roots are equal andis x1=%fandx2=%f",x1,x2);
}
else if(d>0)
{
e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
printf("\n the roots are x1=%fandx2=%f",x1,x2);
}
getch();
}

output:-enter the coefficient=1
-5
6
the root are x1=3.000000 and x2=2.000000

No comments:

Post a Comment