C Program to Find the Roots of a Quadratic Equation

This post explains how to find the roots of a Quadratic Equation using the C programming language. Let’s get started.

C Program to Find the Roots of a Quadratic Equation

#include<math.h>
#include<stdio.h>
int main()
{
    //Program for finding roots of a quadratic equation
    double R1,R2,Dis,a,b,c,realpart,imgpart;
    printf("Enter the coefficient of a,b and c:");
    scanf("%lf %lf %lf", &a,&b,&c);
    Dis=b*b-4*a*c;
    //Rules for real and different roots
    if(Dis>0)
    {
        R1=(-b+sqrt(Dis)/(2*a));
        R2=(-b+sqrt(Dis)/(2*a));
        printf("R1=%.2lf and R2=%.2lf",R1,R2);
    }
    //Checking whether the roots are real and equal
    else if(Dis==0)
    {
        R1=R2=-b/(2*a);
        printf("R1=R2=%.2lf", R1);
    }
    //Rules if the roots are not real
    else
    {
        realpart=-b/(2*a);
        imgpart=sqrt(-Dis)/(2*a);
        printf("R1=%.2lf+%.2lfi and R2=%.2lf-%.2lfi", realpart,imgpart,realpart,imgpart);
    }
    return 0;
}

Output

Explanation

First, let us get to know about quadratic equation, Quadratic Equation in algebraic form is any equation that can be reformed in standard form in which “x” is an unknown number and “a, b and c” are the known numbers, where “a is not equal to 0” and if “a=0” then it’s a linear equation and not a quadratic because there is no numeric term.

Now the main objective for us is to find whether the roots are “real and different(Dis>0)” or “real and equal(Dis=0)” or “complex and different(Dis<0)”. Where we require two roots namely R1 and R2. We need to first get the coefficient for the three variables named “a,b and c”, and finally to check whether the given two roots are not real, we use realpart and imgpart(imaginary). This is calculated with the quadratic term which is displayed below in “Dis”.

 	double R1,R2,Dis,a,b,c,realpart,imgpart;
    printf("Enter the coefficient of a,b and c:");
    scanf("%lf %lf %lf", &a,&b,&c);
    Dis=b*b-4*a*c;

The first if condition is for checking whether the roots are real and different and then the required result is stored in R1 and R2. Here we are using “%.2lf” this means that the value will be rounded off to only two decimal places, and lf means long float

	if(Dis>0)
    {
        R1=(-b+sqrt(Dis)/(2*a));
        R2=(-b+sqrt(Dis)/(2*a));
        printf("R1=%.2lf and R2=%.2lf",R1,R2);
    }

Now we use the if else condition, where we check the roots are real and equal, where R1 is equal to R2 and both the roots, is equal the b/(2*a).

	else if(Dis==0)
    {
        R1=R2=-b/(2*a);
        printf("R1=R2=%.2lf", R1);
    }

We use the else condition to check realpart and imgpart of the roots, in simple words whether the roots are complex and different. In this part R1 and R2 is named as realpart and imgpart respectively. Note that to attain the realpart and imgpart values we should use the formula given in the printf statement where “+” is for realpart and “-“ for imgpart.

	else
    {
        realpart=-b/(2*a);
        imgpart=sqrt(-Dis)/(2*a);
        printf("R1=%.2lf+%.2lfi and R2=%.2lf-%.2lfi", 		realpart,imgpart,realpart,imgpart);
    }

Now the C program has attained its required output.

Leave Your Comment

Liked this content?. Please spread the word :)