Monday, 7 January 2013

Write a c code that will calculate the roots of a quadratic equation Hint: d = sqrt (b 2 -4ac), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a (use sqrt function from cmath.h )


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,disc;
printf("enter a,b&c");
scanf("%f%f%f",&a,&b,&c);
disc=(b*b)-(4*a*c);
if(disc>0)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("x1=%f\nx2=%f",x1,x2);
}
else
printf("invalid values");
getch();

}

No comments:

Post a Comment