Tuesday, 23 June 2015

C PROGRAM TO FIND ALL ROOTS OF QUADRATIC EQUATION



#include <stdio.h>
#include <math.h>
int main()
{
  float a, b, c, determinant, r, r1, r2, img;
  printf("Enter Coefficients of a, b, c : ");
  scanf("%f%f%f",&a,&b,&c);
  determinant=b*b-4*a*c;
  if (determinant>0)
  {
      r1= (-b+sqrt(determinant))/(2*a);
      r2= (-b-sqrt(determinant))/(2*a);
      printf("Roots are : %.2f and %.2f",r1 , r2);
  }
  else if (determinant==0)
  {
    r1 = r2 = -b/(2*a);
    printf("Roots are : %.2f and %.2f", r1, r2);
  }
  else
  {
    r= -b/(2*a);
    img = sqrt(-determinant)/(2*a);
    printf("Roots are : %.2f+%.2fi and %.2f-%.2fi", r, img, r, img);
  }
  return 0;
}

OUTPUT
Enter Coefficients of a, b, c : 5
6
7
Roots are : -0.60+1.02i and -0.60-1.02i