Table of contents
1.
Introduction
2.
What are general Quadratic Equations?
3.
Nature of Roots
4.
Steps to Find the Square Roots of the Quadratic Equation
5.
Design (Algorithm)
6.
Code for Quadratic Equation Program in C
6.1.
Time Complexity
6.2.
Space Complexity
7.
Analysis
8.
Frequently Asked Questions
8.1.
What is a quadratic equation in C programming?
8.2.
What is C quadratic function?
8.3.
What is the quadratic formula in coding?
9.
Conclusion
Last Updated: Mar 26, 2025
Easy

Find the Roots of a Quadratic Equation in Program C

Author Nitika
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C programming, finding the roots of a quadratic equation involves solving an equation in the form ax^2 + bx + c = 0, where a, b, and c are coefficients and x is the variable. The program takes the coefficients as input and calculates the roots using the quadratic formula. It determines the nature of the roots (real, equal, or imaginary) based on the discriminant (b^2 - 4ac) and displays the results. In this article, we will see how to solve this and its code implementation.

Quadratic equation roots program

What are general Quadratic Equations?

Quadratic equations play a significant role in mathematics and various scientific applications. They represent second-degree polynomial equations commonly encountered in physics, engineering, and other fields. 

Quadratic equations are polynomial equations with a degree of 2. They are expressed as:

ax² + bx +c = 0, 

where, a ≠ 0

aand are the coefficient variables of the equation. 

The value of x is used to find the roots of the quadratic equation (a, b). 

In a quadratic equation roots are expressed as:

x = (-b ± (√b2-4ac))/2a

According to this the two roots of the quadratic equation will be 

 (-b -√D)/2a  and (-b+ √D)/2a

Here, 

'b²-4ac' is called the Discriminant(D)

 

The roots of a quadratic equation are expressed in three ways: 

  1. Real & distinct
     
  2. Real & equal
     
  3. Real & imaginary

The nature of the equation is decided by the Discriminant.

Nature of Roots

The discriminant D = b²-4ac, nature of roots are defined as:

If Discriminant > 0 : Real and Distinct,

If Discriminant = 0 : Real and Equal,

If Discriminant < 0 : Real and Imaginary

 

The above table explains that if the Discriminant is greater than 0, the roots will be real & distinct (unequal); if the Discriminant is equal to 0, the roots will be real & equal; lastly, when the Discriminant is less than 0, the roots will be real & imaginary.

Click on the following link to read further: Features of C Language.

Steps to Find the Square Roots of the Quadratic Equation

The following is the algorithm for Quadratic Equation Program in C.
 

  • Declare all the variables used in the equation at the start and take input.

 

  • Find the value of the Discriminant( b²- 4ac ) of the equation and then proceed to find the nature of the quadratic equation roots.

 

  • If the value of the Discriminant is greater than 1, print the roots come out to be real & distinct.

 

  • If the value of Discriminant is equal to 1, print that both roots come out to be real & equal.

 

  • If the value of Discriminant is less than 1, print that both the roots come out to be imaginary.


Stop the program.

Design (Algorithm)

We will design an algorithm to calculate the roots of a quadratic equation. Firstly we will take the three coefficients, i.e., a,b, and c in ax2 + bx + c = 0, as input from the user. Now to determine the nature of roots, we will calculate the value of the discriminant using the formula  b*b - 4 *a*c. Using the if-else conditional statement, if the condition discriminant(D) is greater than zero is true, then the two roots are distinct and real, so we can calculate them using the formula r1 = (-b + sqrt(D)) / (2 * a) and r2 = (-b - sqrt(D)) / (2 * a). If the discriminant is equal to zero, the roots are real and equal, and we will calculate the roots using the formula r1 = -b / (2 * a). If the discriminant is less than zero, then the roots are imaginary. Here the algorithm ends. 

The following image shows a flowchart of the above algorithm.

Flowchart

Code for Quadratic Equation Program in C

Let’s implement quadratic equation program in c :

 

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


int main()
{
    float a, b, c, r1, r2, D;
    printf("Enter 3 values: ");
    scanf("%f %f %f", &a, &b, &c);


//Formula for calculating Discriminant.
	
    D = b * b - 4 * a * c;


    if (D > 0)
    {
    	//real & different roots.
    	
        r1 = (-b + sqrt(D)) / (2 * a);
        r2 = (-b - sqrt(D)) / (2 * a);
        printf("ROOT 1 IS = %f\n", r1); 
        printf("ROOT 2 IS = %f", r2); 
    }
    else if (D == 0)
    {
    	
    	//real & equal roots.
    	
        r1 = -b / (2 * a);
        r2 = r1; 
        printf("ROOT 1 IS = %f\n", r1); 
        printf("ROOT 2 IS = %f", r2); 
    }
    else
    {
        printf("REAL ROOTS DO NOT EXIST\n"); 
    }


    return 0; 
}
You can also try this code with Online C Compiler
Run Code

Sqrt() is used to find the square root of a number.

Output 

Enter 3 values: 5 80 125
ROOT 1 IS = -1.755002
ROOT 2 IS = -14.244998

 

Enter 3 values: 1 3 4
REAL ROOTS DO NOT EXIST

Time Complexity

O(1), Here all the computations are done in a constant time hence the time complexity is O(1).

Space Complexity

O(1), Here we are not using any extra space hence the space complexity is  O(1)

Also see, C Static Function

Analysis

As we discussed, the different nature of roots is based on the discriminant's value. The discriminant (D) of a quadratic equation ax^2 + bx + c = 0 determines the nature of its roots. 'b²-4ac' is called the Discriminant(D). Let us analyze the nature of roots with examples and formulas.

  • If D > 0, the equation has two distinct real roots.
    For Example, for the quadratic equation 2x^2 - 5x + 3 = 0, the value of discriminant is, D = (-5)^2 - 4(2)(3) = 1. The Discriminant(D) is greater than 0 and has two distinct real roots. We will calculate roots using the formula x = (-b ± (√D))/2a. So the two roots are x = (-(-5) ± (1))/2(2)), i.e. x = 1 and x = 1.5.
     
  • If D = 0, the equation has two equal real roots.
    For Example, for the quadratic equation x^2 - 4x + 4 = 0, the value of discriminant is, D = (-4)^2 - 4(1)(4) = 0. The Discriminant(D) is equal to 0, so it has two equal real roots. We will calculate the unique root using the formula x = (-b ± (√D))/2a. So the value of root is x = 2 x = (-(-4) ± (0))/2(1)), i.e. x = 2.
     

If D < 0, the equation has no real roots but two complex conjugate roots.
For Example, for the quadratic equation 3x^2 + 2x + 5 = 0, the value of discriminant is, D = 2^2 - 4(3)(5) = -56. As the value of Discriminat is less than zero, there are no real roots for the given equation.

Also read about Tribonacci Series and Short int in C Programming

Frequently Asked Questions

What is a quadratic equation in C programming?

In C programming, a quadratic equation is typically represented as ax2+bx+c=0 where a, b, and c are coefficients.

What is C quadratic function?

A C quadratic function calculates the roots of a quadratic equation using the coefficients a, b, and c provided as inputs.

What is the quadratic formula in coding?

The quadratic formula in coding is x = (-b ± (√D))/2a​​, used to find the roots of the equation ax2+bx+c=0.

Conclusion

In this article, we explored various things related to Quadratic Equation Program in C, leaving no stone unturned. We studied general quadratic equations, roots of the equation, nature of the quadratic equation, steps, algorithm and flowchart of the Quadratic Equation Program in C. 
 

Live masterclass