Tuesday, 9 April 2013

c program to find the roots of the quadratic equation



To find the roots of the quadratic equation


#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,d;
clrscr();
printf("Enter the values for equation:");
scanf("%f%f%f",&a,&b,&c);
/* check the condition */
if(a==0)
printf("Enter value should not be zero ");
else
{
             d=b*b-4*a*c;
/* check the condition */
  if(d>0)
            {
                        r1=(-b+sqrt(d)/(2*a));
                         r2=(-b-sqrt(d)/(2*a));
                         printf("roots are real and unequal\n");
                         printf("%f\n%f\n",r1,r2);
             }
  else
  if(d==0)
            {
                        r1=-b/(2*a);
                         r2=-b/(2*a);
                         printf("roots are real and equal\n");
                         printf("root=%f\n",r1);
                          printf("root=%f\n",r2);
  }
             else
                         printf("roots are imaginary");
    }
    getch();
}

No comments: