Solving Quadratic Equation

Submitted by Karthikeyan on Sat, 05/09/2009 - 06:09

C Sample Program : Solving Quadratic Equation. This program receives the coefficient of a quadratic equation and calculates output


#include 
#include
#include  
main()
   {
      float  a, b, c, d,/*d for discriminant*/
             root1, root2;
	
	  printf("\nInput values of a, b, and c\n");
      scanf("%f %f %f", &a, &b, &c);

      d = b*b - 4*a*c ;

      if(d < 0)
      {
         printf("\n\nROOTS ARE IMAGINARY\n");
         root1 = -b /(2*a);
         root2 = sqrt(-d)/(2*a);
         printf("\nThe Real part      Root1 = %5.2f", root1);
         printf("\nThe imaginary part Root2 = %5.2f",root2);
      }
      else if(d == 0)
      {
        printf("\n THE ROOTS ARE REAL AND EQUAL");
        root1 = -b / (2*a);
        root2 = -b / (2*a);
        printf("\n Root1 = %f\n Root2 = %f",root1, root2);
      }
      else
      {
         root1 = (-b + sqrt(d))/(2.0*a);
         root2 = (-b - sqrt(d))/(2.0*a);
         printf("The ROOTS ARE REAL AND UNEQUAL \n");
         printf("\n\nRoot1 = %5.2f\n\nRoot2 = %5.2f\n",
                     root1,root2 );
	   }
	
   }