/*------------------------------------------------------------*/
/*  Program solves quadratic equation                                          
 *  
 *  Given a*x*x + b*x + c = 0
 *  This program computes x based on
 *  1. if a = 0
 *     there is one solution for x. (x= -c/b).
 *  2. if a != 0 then you use the Discriminate to determine x
 *   given that d = 4ac -b*b
 *   then the Discriminate is -d
 *   if d<0
 *   then A. there are two roots
 *			x= (-b + sqrt( -d ) )/(2*a)
 *		   and
 *			x= (-b - sqrt( -d ) )/(2*a)
 *	else B. if d==0 there is one root
 *			x= (-b)/(2*a)
 *	else C. if d>0 there are no roots.
 */
/*------------------------------------------------------------*/
#include <stdio.h>
#include <math.h> /* required here due to sqrt function */

int main(void)
{
   /*  Declare variables.  If needed, you can declare more*/
   double a, b, c, d, x1, x2;
   
         
 
   /*  Enter a b c */
   printf("Enter a b c of a*x*x + b*x + c = 0 : ");
   scanf("%lf %lf %lf",&a, &b, &c);
   
   /* if a = 0 there is a linear solution */

   if(a == 0){
	   if( b == 0 ) printf("Invalid eqution no output\n");
	   else{
		   x1 = -c/b;
		   printf("There is a linear solution x=%lf\n",x1);
	   }
   }else {    /*  Compute d */ 
	   d=4*a*c - b*b;
	   if( d<0)
	   {
	   /* There are two roots */
		   x1= (-b + sqrt( -d ) )/(2.0*a);
		   x2= (-b - sqrt( -d ) )/(2.0*a);
		   printf("There are two real roots:\n"
		   "x1=%lf\n x2=%lf\n", x1, x2);
	   }
	   else if(d==0){
		   	   /* There is one root */
		   x1= (-b)/(2.0*a);
		   printf("There is one real root:\n x1=%lf\n",x1);
	   }
	   else if(d>0){
		   	   /* There are no roots */
		   printf("There are no real roots.\n");
	   }
   }
   /*  Exit program.  */
   return 0;
}

/*
Enter a b c of a*x*x + b*x + c = 0 : 0 1 2
There is a linear solution x=-2.000000

Enter a b c of a*x*x + b*x + c = 0 : 1 9 2
There are two real roots:
x1=-0.227998
 x2=-8.772002

Enter a b c of a*x*x + b*x + c = 0 : 1 2 2
There are no real roots.

Enter a b c of a*x*x + b*x + c = 0 : 1 2 1
There is one real root:
 x1=-1.000000
*/