C Program to calculate roots of a quadratic equation using switch statement

/*WAP  to calculate roots of a quadratic equation using switch statement*/
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
  int option;
  float a,b,c,d,r1,r2;
  clrscr();
  printf("Enter The Coefficient Of A, B and C\n");\
  scanf("%f%f%f",&a,&b,&c);
  if(a==0)
  {
     printf("Not A Quadratic Equation\n");
     exit(0);
   }
  d=b*b-4*a*c;
  if(d==0)
  option=1;
   else
  if(d>0)
  option=2;
   else
  option=3;
  switch(option)
  {
     case 1:printf("Roots Are Real and Equal \n");
		  r1=r2=-b/2*a;
		  printf("ROOT1=%f\n",r1);
		  printf("ROOT2=%f\n",r2);
		  break;
     case 2:printf("Equation Is Real and Distinct\n");
		  r1=-b+sqrt(d)/(2*a);
		  r2=-b-sqrt(d)/(2*a);
		  printf("ROOT1=%f\n",r1);
		  printf("ROOT2=%f\n",r2);
		  break;
     case 3:printf("Imaginary Roots \n");
		  r1=-b/2*a;
		  r2=sqrt(abs(b))/(2*a);
		  printf("ROOT1=%f+i%f\n",r1,r2);
		  printf("ROOT2=%f-i%f\n",r1,r2);
   }
  getch();
}


Output

Enter The Coefficient Of A, B and C
0 1 2
Not A Quadratic Equation

Enter The Coefficient Of A, B and C
2 23  33
Equation Is Real and Distinct
ROOT1=-18.930294
ROOT2=-27.069706

Enter The Coefficient Of A, B and C
21 25 26
Imaginary Roots
ROOT1=-262.500000+i0.119048
ROOT2=-262.500000-i0.119048

Enter The Coefficient Of A, B and C
9 0 0
Roots Are Real and Equal
ROOT1=-0.000000
ROOT2=-0.000000

Download Program

Download