---
title: "Roots of quadratic equation in C"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:quadratic-roots-c"
source: https://www.bctengineer.com/q/semester_1/computer_programming/quadratic-roots-c
site: BCT Engineering
---

# Roots of quadratic equation in C

Computer Programming · Chapter 5: Control statements

- Asked in: 2069 Asar, 8 marks
- Video: [C Program to find roots of quadratic equation | C Programming tutorials](https://www.youtube.com/watch?v=R2ZmqsM3lZQ) — Vaibhav Talks - Coding
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/quadratic-roots-c
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

Write a program in C to find all possible roots of a quadratic equation ax²+bx+c=0.

## Short answer

Compute discriminant d = b^2 - 4ac. If d>0, real distinct roots (-b±√d)/(2a); d=0, one real root -b/(2a); d<0, complex -b/(2a) ± i√(-d)/(2a). Handle a=0.

## Answer

For ax² + bx + c = 0, the discriminant is d = b² - 4ac, and the roots are x = (-b ± √d) / 2a.
- d > 0: two real and distinct roots
- d = 0: two real and equal roots, x = -b / 2a
- d < 0: two complex roots, -b/2a ± i √(-d)/2a

```c
#include <stdio.h>
#include <math.h>
int main(void)
{
    float a, b, c, d, r1, r2, re, im;
    printf("Enter a, b, c: ");
    scanf("%f %f %f", &a, &b, &c);
    if (a == 0)
    {
        if (b != 0)
            printf("Linear equation, root = %.2f\n", -c / b);
        else
            printf("Not an equation\n");
        return 0;
    }
    d = b * b - 4 * a * c;
    if (d > 0)
    {
        r1 = (-b + sqrt(d)) / (2 * a);
        r2 = (-b - sqrt(d)) / (2 * a);
        printf("Real and distinct roots: %.2f and %.2f\n", r1, r2);
    }
    else if (d == 0)
    {
        r1 = -b / (2 * a);
        printf("Real and equal roots: %.2f and %.2f\n", r1, r1);
    }
    else
    {
        re = -b / (2 * a);
        im = sqrt(-d) / (2 * a);
        printf("Complex roots: %.2f + %.2fi and %.2f - %.2fi\n", re, im, re, im);
    }
    return 0;
}
```
Output for a = 1, b = 2, c = 5:
```text
Enter a, b, c: Complex roots: -1.00 + 2.00i and -1.00 - 2.00i
```

Other test runs: a = 1, b = -5, c = 6 gives `Real and distinct roots: 3.00 and 2.00`; a = 1, b = -2, c = 1 gives `Real and equal roots: 1.00 and 1.00`.

The program also handles a = 0 (the equation is linear) so there is no division by zero. Compile with `-lm` for `sqrt`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Discriminant formula?
   A. b^2 + 4ac
   B. b^2 - 4ac
   C. 4ac - b^2
   D. b - 4ac
   Answer: B. d = b^2-4ac. (Key point: Discriminant)

2. d = 0 means?
   A. Complex
   B. Two distinct
   C. Two real equal roots
   D. No roots
   Answer: C. x=-b/2a. (Key point: d=0)

3. d < 0 gives?
   A. Real roots
   B. No roots
   C. One root
   D. Complex roots -b/2a ± i sqrt(-d)/2a
   Answer: D. Imaginary part. (Key point: d<0)

4. Why check a==0?
   A. Linear equation; avoid division by zero
   B. Skip
   C. Complex
   D. Speed
   Answer: A. 2a would be zero. (Key point: a=0)

## Related in Computer Programming

- [Quadratic equation roots in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-quadratic-roots) — Computer Programming, chapter 11, asked 2×
- [Algorithm and flowchart: quadratic roots](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgo-flowchart-quadratic) — Computer Programming, chapter 2

## Related in other subjects

- [Test cases for a quadratic equation program](https://www.bctengineer.com/question.md?key=semester_5%2Fsoftware_engineering%3Aquadratic-test-cases) — Software Engineering, chapter 8
- [DUIS: ∫ (e^(−ax) − e^(−bx))/x dx](https://www.bctengineer.com/question.md?key=semester_1%2Fmathematics_i%3Aduis-e-ax-minus-e-bx) — Mathematics I, chapter 2, asked 9×
- [K range for roots left of s = −2](https://www.bctengineer.com/question.md?key=semester_3%2Fcontrol_systems%3Arh-k-s10-s-s3-left) — Control Systems, chapter 4
