0
0
CProgramBeginner · 2 min read

C Program to Solve Quadratic Equation with Output

A C program to solve a quadratic equation uses the formula discriminant = b*b - 4*a*c and calculates roots with root1 = (-b + sqrt(discriminant)) / (2*a) and root2 = (-b - sqrt(discriminant)) / (2*a) depending on the discriminant value.
📋

Examples

Inputa=1, b=-3, c=2
OutputRoots are real and different: 2.00 and 1.00
Inputa=1, b=2, c=1
OutputRoots are real and same: -1.00 and -1.00
Inputa=1, b=2, c=5
OutputRoots are complex and different: -1.00 + 2.00i and -1.00 - 2.00i
🧠

How to Think About It

To solve a quadratic equation, first calculate the discriminant using b*b - 4*a*c. If the discriminant is positive, roots are two different real numbers. If zero, roots are equal real numbers. If negative, roots are complex numbers with real and imaginary parts.
📐

Algorithm

1
Get input values for coefficients a, b, and c
2
Calculate discriminant = b*b - 4*a*c
3
If discriminant > 0, calculate two real and different roots
4
If discriminant == 0, calculate one real root repeated twice
5
If discriminant < 0, calculate complex roots with real and imaginary parts
6
Print the roots accordingly
💻

Code

c
#include <stdio.h>
#include <math.h>

int main() {
    double a, b, c, discriminant, root1, root2, realPart, imagPart;
    printf("Enter coefficients a, b and c: ");
    scanf("%lf %lf %lf", &a, &b, &c);

    if (a == 0) {
        printf("Coefficient 'a' cannot be zero for a quadratic equation.\n");
        return 1;
    }

    discriminant = b*b - 4*a*c;

    if (discriminant > 0) {
        root1 = (-b + sqrt(discriminant)) / (2*a);
        root2 = (-b - sqrt(discriminant)) / (2*a);
        printf("Roots are real and different: %.2lf and %.2lf\n", root1, root2);
    } else if (discriminant == 0) {
        root1 = -b / (2*a);
        printf("Roots are real and same: %.2lf and %.2lf\n", root1, root1);
    } else {
        realPart = -b / (2*a);
        imagPart = sqrt(-discriminant) / (2*a);
        printf("Roots are complex and different: %.2lf + %.2lfi and %.2lf - %.2lfi\n", realPart, imagPart, realPart, imagPart);
    }
    return 0;
}
Output
Enter coefficients a, b and c: 1 -3 2 Roots are real and different: 2.00 and 1.00
🔍

Dry Run

Let's trace the input a=1, b=-3, c=2 through the code

1

Input coefficients

a=1, b=-3, c=2

2

Calculate discriminant

discriminant = (-3)*(-3) - 4*1*2 = 9 - 8 = 1

3

Check discriminant > 0

1 > 0 is true, so roots are real and different

4

Calculate roots

root1 = (-(-3) + sqrt(1)) / (2*1) = (3 + 1)/2 = 2 root2 = (3 - 1)/2 = 1

5

Print roots

Roots are real and different: 2.00 and 1.00

StepVariableValue
2discriminant1
4root12
4root21
💡

Why This Works

Step 1: Calculate discriminant

The discriminant b*b - 4*a*c tells us the nature of roots: positive means two real roots, zero means one real root repeated, negative means complex roots.

Step 2: Compute roots based on discriminant

If discriminant is positive or zero, roots are calculated using the quadratic formula with square root of discriminant. If negative, roots have real and imaginary parts.

Step 3: Print roots

Roots are printed with two decimal places for clarity, showing whether they are real and different, real and same, or complex.

🔄

Alternative Approaches

Using complex.h library for complex roots
c
#include <stdio.h>
#include <complex.h>

int main() {
    double a, b, c;
    double complex root1, root2;
    printf("Enter coefficients a, b and c: ");
    scanf("%lf %lf %lf", &a, &b, &c);

    if (a == 0) {
        printf("Coefficient 'a' cannot be zero for a quadratic equation.\n");
        return 1;
    }

    double complex discriminant = csqrt(b*b - 4*a*c);
    root1 = (-b + discriminant) / (2*a);
    root2 = (-b - discriminant) / (2*a);

    printf("Roots are: %.2lf%+.2lfi and %.2lf%+.2lfi\n", creal(root1), cimag(root1), creal(root2), cimag(root2));
    return 0;
}
This approach uses the <complex.h> library to handle complex roots automatically, simplifying code but requiring C99 support.
Using separate functions for discriminant and roots
c
#include <stdio.h>
#include <math.h>

double discriminant(double a, double b, double c) {
    return b*b - 4*a*c;
}

void findRoots(double a, double b, double c) {
    if (a == 0) {
        printf("Coefficient 'a' cannot be zero for a quadratic equation.\n");
        return;
    }
    double disc = discriminant(a,b,c);
    if (disc > 0) {
        printf("Roots are real and different: %.2lf and %.2lf\n", (-b+sqrt(disc))/(2*a), (-b-sqrt(disc))/(2*a));
    } else if (disc == 0) {
        printf("Roots are real and same: %.2lf and %.2lf\n", -b/(2*a), -b/(2*a));
    } else {
        double real = -b/(2*a);
        double imag = sqrt(-disc)/(2*a);
        printf("Roots are complex and different: %.2lf + %.2lfi and %.2lf - %.2lfi\n", real, imag, real, imag);
    }
}

int main() {
    double a,b,c;
    printf("Enter coefficients a, b and c: ");
    scanf("%lf %lf %lf", &a, &b, &c);
    findRoots(a,b,c);
    return 0;
}
This approach improves readability by separating logic into functions but adds slight overhead.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic operations and conditional checks, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store inputs and results, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time, but using simplifies code for complex roots at the cost of requiring C99 support.

ApproachTimeSpaceBest For
Basic if-else with math.hO(1)O(1)Simple and portable code
Using complex.h libraryO(1)O(1)Automatic complex root handling, requires C99
Separate functionsO(1)O(1)Improved readability and modularity
💡
Always check if coefficient 'a' is zero before solving, as it would not be a quadratic equation.
⚠️
Beginners often forget to handle the case when discriminant is negative, causing incorrect root calculations.