Sunday, October 23, 2005

roots of a quadratic equation

Program to find the roots of a quadratic equation

 

 

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

 

int main (void)

 

 

{

 int a; int b; int c; double x1; double x2; double E; int E1; double

R; double I;double S;

    char q; char Y;

 

 

 printf ("Please key in the value of constant a,b and c for finding

the roots of quadratic");

    printf ("equation ax%c+bx+c=0  :",253);

 scanf ("%d%d%d", &a,&b,&c);

 

 

    E  =(b*b)-(4*a*c);

 

 

   if ( E > 0)

   {    

    x1 = (float)(-b+sqrt(E))/(2*a);

    x2 = (float)(-b-sqrt(E))/(2*a);

 

 

  printf ("\nYour quadratic equation has two distinct real roots:

x1=%1.6f ,x2=%1.6f",x1,x2);

   }

 

 

   else if (E == 0)

   {

 

 

      x1 = (float)(-b+sqrt(E))/(2*a);

 

 

   printf ("\nYour quadratic equation has two same:

x1=x2=%1.6f\n",x1);

   }

 

 

   else

 

 

   {

 

 

  printf ("Your quadratic equation has two distinct imaginary roots. 

Do you want to know");

  printf ("\nthe values of the imaginary roots (Y/N)?");

 

 

  scanf ("%c",&q);              /* Dummy.  The computer jumps this scanf() */

 

 

  scanf ("%c",&q);

 

 

  printf ("q = %c\n",q);       /* Test statement*/

 

 

  if ('Y'==q)

  {

      R = (float)-b/(2*a);

      S=abs(E);

   S=sqrt(S);

   I = S/(2*a);

      printf ("\nThe imaginary roots are:\n");

      printf (" x1=%1.6f + %1.6fi , x2=%1.6f - %1.6fi\n",R,I,R,I);

  }

  else

   printf ("Thank you for using this computer\n");

 

 

 

}

  return 0;

}

 

 

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India

 

No comments: