Q.Write a C program to find the roots of the quadratic equation ax^2 + bx + c = 0 using the switch-case statement.
The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
/*program to find the square root of the given quadratic equation using switch case */
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int choice;
float a,b,c,d,i,rpart,ipart,r1,r2;
clrscr();
printf("Enter the values of a,b,c,\n");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
choice=1;
}
else
if(d==0)
{
choice=2;
}
else
{
choice=3;
}
switch(choice)
{
case 1:printf("The roots are real and unequal\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("r1=%.3f\nr2=%.3f",r1,r2);
break;
case 2:printf("The roots are equal\n");
r1=b/(2*a);
r2=r1;
printf("r1 and r2 are %.3f\n",r1);
break;
case 3:printf("The roots are imaginary\n");
d=sqrt(abs(d));
rpart=-b/(2*a);
ipart=d/(2*a);
printf("r1=%.3f+i%.3f\n",rpart,ipart);
printf("r2=%.3f-i%.3f\n",rpart,ipart);
}
getch();
}
/*program to find the square root of the given quadratic equation using switch case */
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int choice;
float a,b,c,d,i,rpart,ipart,r1,r2;
clrscr();
printf("Enter the values of a,b,c,\n");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
choice=1;
}
else
if(d==0)
{
choice=2;
}
else
{
choice=3;
}
switch(choice)
{
case 1:printf("The roots are real and unequal\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("r1=%.3f\nr2=%.3f",r1,r2);
break;
case 2:printf("The roots are equal\n");
r1=b/(2*a);
r2=r1;
printf("r1 and r2 are %.3f\n",r1);
break;
case 3:printf("The roots are imaginary\n");
d=sqrt(abs(d));
rpart=-b/(2*a);
ipart=d/(2*a);
printf("r1=%.3f+i%.3f\n",rpart,ipart);
printf("r2=%.3f-i%.3f\n",rpart,ipart);
}
getch();
}
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
Classify by the discriminant, then a switch prints real-unequal, equal, or imaginary roots; a=1,b=-5,c=6 gives r1=3, r2=2.
Solving a quadratic is a natural three-way decision. The discriminant d = b^2 - 4ac fixes the nature of the roots: positive means two distinct real roots from the quadratic formula (-b +/- sqrt(d))/(2a), zero means a single repeated real root -b/(2a), and negative means a complex conjugate pair with real part -b/(2a) and imaginary part sqrt(|d|)/(2a). The program maps these three outcomes to choice 1/2/3 and a switch selects the matching formula; the library function sqrt() (from math.h) supplies the square root. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.