Saturday 23 April 2011

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


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float a,b,c,root1,root2;
clrscr();
printf("\n Enter values of a,b,c for finding roots of a quadratic (ax2+bx+c)eq:\n");
scanf("%f%f%f",&a,&b,&c);
/*checking condition*/
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("\nROOTS ARE\n");
printf("\n root1=%f\n root2=%f",root1,root2);
}
else
printf("\n Imaginary Roots.");
getch();
return 0;
}


OutPut:
Enter values of a,b,c for finding roots of a quadratic (ax2+bx+c)eq:
2 -7 5
ROOTS ARE
root1=2.500000
root2=1.000000

No comments:

Post a Comment