Write a Program to find the root of the given quadratic equation using switch case.

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
    int flag;
    float a,b,c,d;
    float x,x1,x2;
    float rpart,ipart;
    clrscr();
    printf("\Enter 3 NUmber:");
    scanf("%f%f%f",&a,&b,&c);
    if(a==0)
    {
      x=-c/b;
      printf("Only root x=%7.3f",x);
      exit();
    }
    d=((b*b)-(4*a*c));
    if(d>0)
       flag=1;
       else if(d==0)
      flag=2;
      else
          flag=3;
    switch(flag)
    {
    case 1: printf("\nRead and Distinct roots are :");
        x1=(-b+sqrt(d))/(2*a);
        x2=(-b-sqrt(d))/(2*a);
        printf("\nx1=%7.3f\nx2=%f",x1,x2);
        break;
    case 2: printf("\nRepeated Roots are :");
        x1=-b/(2*a);
        x2=x1;
        printf("\nx1 and x2=%f",x1);
        break;
    case 3: d=sqrt(abs(d));
        rpart=-b/(2*a);
        ipart=d/(2*a);
        printf("\nComplex Roots are ");
        printf("\nx1=%7.3f -i%f",rpart,ipart);
        printf("\nx2=%f -i%f",rpart,ipart);
        break;
    }
    getch();
    return;
}

No comments:

Post a Comment