Answer E22

quadratic formula
#include <stdio.h>
#include <math.h>

/* Puzzle E22 -- math to C, Yet Again*/
 
int main()
{
  double a = 1.4, b = 4.2, c = -2.4;
  double discrim, result;
  
  discrim =  b*b - 4*a*c ;  /* intermediate value */
  
  if   ( discrim >= 0 )
  {
    result = (-b + sqrt(discrim) )/(2*a) ;  /* Both sets of () are needed */
  
    printf("result: %f\n", result ); 
  }
  else
  {
    printf("No real number solution\n" );   
  }
  return 0;
}

For additional practice, you might wish to extend the program so that it asks the user for the a, b, and c of a quadratic equation and then determines both real solutions if they exist. This is a common first program for beginning programmers.



Back to Puzzle Home