Skip to content
Question Bank (5 marks) · Q14

Q.Write a C program to find the roots of a quadratic equation ax²+bx+c=0 using switch statement.

Karnataka PUCTextbookLong· 5mImportance★★★★★est
47% · 73/154 Questions
🔒 Locked · start free trial →

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]

Compute d = b*b-4ac, map its sign to a choice (1/2/3), and use switch to print real-unequal, real-equal or imaginary roots.

For ax2+bx+c=0ax^{2}+bx+c=0 the discriminant d=b2−4acd=b^{2}-4ac decides the nature of the roots. The sign of d is converted into a choice (1, 2 or 3) and a switch statement handles each case using the quadratic formula x=−b±d2ax=\dfrac{-b\pm\sqrt{d}}{2a}.

#include <stdio.h>
#include <math.h>
int main()
{
    float a, b, c, d, r1, r2, real, imag;
    int choice;
    printf("Enter the coefficients a, b, c: ");
    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:
            r1 = (-b + sqrt(d)) / (2 * a);
            r2 = (-b - sqrt(d)) / (2 * a);
            printf("Roots are real and unequal\n");
            printf("r1 = %.3f, r2 = %.3f\n", r1, r2);
            break;
        case 2:
            r1 = -b / (2 * a);
            printf("Roots are real and equal\n");
            printf("r1 = r2 = %.3f\n", r1);
            break;
        case 3:
            real = -b / (2 * a);
            imag = sqrt(-d) / (2 * a); …

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.