Subscribe

RSS Feed (xml)

Tuesday, January 6, 2009

Write a C program to find the roots of a quadratic equation

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
int main(void)
{
double a, b, c;
double root1, root2;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
if(b*b > 4*a*c)
{
root1= (-b + sqrt(b*b -(4*a*c)))/(2*a);
root2= (-b - sqrt(b*b -(4*a*c)))/(2*a);
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
printf("\nImaginary roots");
getch( );

return 0;
}

No comments:

Post a Comment