C Program to Find the Roots of a Quadratic Equation

By | August 29, 2020

In this article, you will learn to find the roots of a quadratic equation in C programming.

Quadratic Equation :

In elementary algebra a quadratic equation is an equation in the form of

ax2 + bx + c = 0 

It is known as a quadratic equation, where a, b and c are numbers and a is not equal to 0. The values of x which satisfies the equation are known as the roots of the equation.

Solving Quadratic Equation :

Below is direct formula for finding roots of quadratic equation.

There are following important cases.

  1. If b*b < 4*a*c, then roots are complex (not real).
    For example roots of x2 + x + 1, roots are -0.5 + i1.73205 and -0.5 – i1.73205

  2. If b*b == 4*a*c, then roots are real and both roots are same.
    For example, roots of x2 – 2x + 1 are 1 and 1

  3. If b*b > 4*a*c, then roots are real and different.
    For example, roots of x2 – 7x – 12 are 3 and 4

Algorithm :

Based on the above formula let us write step by step descriptive logic to find roots of a quadratic equation.

  1. Take input a, b and c.
  2. Find
    discriminant = (b*b) - (4*a*c)  
  3. Compute roots based on the nature of discriminant.
  4. If discriminant > 0 then,
    root1 = (-b + sqrt(discriminant)) / (2*a) and
    root2 = (-b - sqrt(discriminant)) / (2*a). 
  5. If discriminant == 0 then,
    root1 = root2 = -b / (2*a) 
  6. Else if discriminant < 0 then, there are two distinct complex roots where
    root1 = -b / (2*a) , and 
    root2 = -b / (2*a) 
  7. Imaginary part of the root is given by imaginary
    = sqrt(-discriminant) / (2*a) 

Example :

Input a: 1
Input b: 7
Input c: 12

Output :
Root1: -3
Root2: -4 

Implementation :

// C program to find all roots of a quadratic equation
#include <stdio.h>
// Used for sqrt()
#include <math.h> 

int main()
{
    // take input 
    float a = 1, b = 7, c = 12;
    
    
    float root1, root2, imaginary;
    float discriminant;
    
    
    // Find discriminant of the equation 
    discriminant = (b * b) - (4 * a * c);
    
   
    // Find the nature of discriminant 
    if(discriminant > 0)
    {
        root1 = (-b + sqrt(discriminant)) / (2*a);
        root2 = (-b - sqrt(discriminant)) / (2*a);

        printf("Distinct real roots : %.2f and %.2f", 
                                     root1, root2);
    }
    else if(discriminant == 0)
    {
        root1 = root2 = -b / (2 * a);

        printf("Same real roots : %.2f and %.2f", 
                                     root1, root2);
    }
    else if(discriminant < 0)
    {
        root1 = root2 = -b / (2 * a);
        imaginary = sqrt(-discriminant) / (2 * a);

 printf("Distinct complex roots : %.2f+i%.2f and %.2f-i%.2f", 
                   root1, imaginary, root2, imaginary);
    }

    // successful complemetion
    return 0;
}

Output :

Distinct real roots : -3.00 and -4.00  



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.