✓ Link copied to clipboard
C 📅 July 3, 2026 ⏱ 6 min read

C Program to Find the Roots of a Quadratic Equation

C Program to Find the Roots of a Quadratic Equation

Finding the roots of a quadratic equation isn’t as tough as it seems. Take the equation ax² + bx + c = 0. You’ll run into this a lot in exams or real-life stuff. Let’s check out how to figure them out using C.

Here’s a quick example — for the equation x² + 5x + 6 = 0, the roots are -2 and -3. Now, let’s jump into the code that makes this happen.

Prerequisites for Finding the Roots Program in C

Before coding, make sure you know the following:

  • Understanding of quadratic equations and the quadratic formula
  • Using math.h for functions like sqrt
  • Basic if...else conditional statements
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Find the Roots of a Quadratic Equation

Here's the complete code to find the roots of a quadratic equation using the quadratic formula. It's a great example of conditional logic in action.

find_roots.cC▶ Run
#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
    // Accept coefficients from the user
    printf("Enter coefficients a, b and c: ");
    scanf("%f %f %f", &a, &b, &c);

    // Calculate the discriminant
    discriminant = b*b - 4*a*c;

    // Check the nature of the discriminant
    if (discriminant > 0) {
        root1 = (-b + sqrt(discriminant)) / (2*a);
        root2 = (-b - sqrt(discriminant)) / (2*a);
        printf("Root1 = %.2f\n", root1);
        printf("Root2 = %.2f\n", root2);
    }
    else if (discriminant == 0) {
        root1 = root2 = -b / (2*a);
        printf("Root1 = Root2 = %.2f\n", root1);
    }
    else {
        realPart = -b / (2*a);
        imaginaryPart = sqrt(-discriminant) / (2*a);
        printf("Root1 = %.2f + %.2fi\n", realPart, imaginaryPart);
        printf("Root2 = %.2f - %.2fi\n", realPart, imaginaryPart);
    }

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Find the Roots Program

Here's what you'll see when you run the program. We used the coefficients 1, 5, and 6 in this example.

OUTPUTTerminal
Enter coefficients a, b and c: 1 5 6
Root1 = -2.00
Root2 = -3.00
Output of C Program to Find the Roots of a Quadratic Equation
Output of C Program to Find the Roots of a Quadratic Equation

Algorithm to Find the Roots of a Quadratic Equation in C

Finding the roots involves calculating the discriminant and using the quadratic formula. Here is the exact logic, step by step.

  1. Read the coefficients a, b, and c from the user.
  2. Calculate the discriminant using the formula d = b*b - 4*a*c.
  3. If the discriminant is greater than 0, calculate two distinct real roots using the formulas.
  4. If the discriminant is 0, calculate one real root.
  5. If the discriminant is less than 0, calculate the real and imaginary parts of the complex roots.
  6. Display the roots accordingly.
flowchart TD
  A([Start]) --> B[/Read coefficients a, b, c/]
  B --> C[Calculate discriminant (d)]
  C --> D{"d > 0?"}
  D -->|Yes| E[Two real & distinct roots]
  E --> F["root1 = (-b + sqrt(d)) / (2*a)"]
  F --> G["root2 = (-b - sqrt(d)) / (2*a)"]
  G --> H[/Display root1 and root2/]
  D -->|No| I{"d == 0?"}
  I -->|Yes| J["root1 = root2 = -b / (2*a)"]
  J --> K[/Display root1/]
  I -->|No| L["Two complex roots"]
  L --> M[Calculate real and imaginary parts]
  M --> N[/Display complex roots/]
  H --> O([End])
  K --> O
  N --> O
Flowchart: the algorithm step by step

How the C Program to Find Roots Works

Let's break down the code line by line to understand how it works.

  • #include <stdio.h> — Imports the standard input/output library for printf and scanf.
  • #include <math.h> — Imports the math library for mathematical functions like sqrt.
  • int main() — Main function where execution begins.
  • float a, b, c, ... — Declare variables to store coefficients and roots.
  • scanf("%f %f %f", &a, &b, &c) — Reads input for coefficients a, b, and c.
  • discriminant = b*b - 4*a*c — Calculates the discriminant to decide the nature of the roots.
  • if (discriminant > 0) — Conditional check for two real and distinct roots.
  • root1 = (-b + sqrt(discriminant)) / (2*a) — Calculates the first root.
  • root2 = (-b - sqrt(discriminant)) / (2*a) — Calculates the second root.
  • else if (discriminant == 0) — Checks for a single real root.
  • root1 = root2 = -b / (2*a) — Single real root.
  • else — Handles complex roots with real and imaginary parts.
  • return 0 — Ends the program.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What does the discriminant d determine in this program?

🎉Correct! The discriminant determines whether the roots are real and distinct, real and equal, or complex.
🤔Not quite. Think about how the discriminant affects the formulas for the roots.

Time and Space Complexity of the Quadratic Equation Program

Understanding the efficiency of this program is essential.

Time Complexity: O(1). The operations are fixed and do not depend on the size of input, making the execution time constant.

Space Complexity: O(1). The program uses a constant amount of memory, storing only a fixed number of variables like a, b, c.

⚠️

Common mistake: Forgetting to include math.h can cause errors when trying to use sqrt().

Key takeaway: This program shows how important the discriminant is for figuring out the nature of quadratic roots.

FAQs About Finding Roots of Quadratic Equations in C

Here are some common questions about this program.

The program can find two distinct real roots, a single real root, or two complex roots based on the discriminant.
math.h gives you the sqrt() function, which is key for calculating square roots.
When the discriminant is negative, it calculates complex roots using real and imaginary parts.
No issue, the code handles negative discriminants and shows complex roots.
This code is for quadratic equations only, not other polynomial types.

If you found this program helpful, check out these related programs:

Conclusion

Got quadratic equations to solve? This program makes it a breeze. You’ll find this approach useful in plenty of math applications.

Want to explore more? Check out our C programming quiz or run your own tests in the online compiler.