C Program To Find The Roots of a Quadratic Equation

In this post, you will learn how to find the roots of a quadratic equation using C Programming language.

Quadratic Equation is any equation that can be rearranged in the standard form as ax[2] + bx = c where a,b and c are real numbers and a is not equal to 0.

C Program To Find The Roots of a Quadratic Equation

The discriminant is equal to b2 – 4ac . It tells the nature of the root.

  • If the discriminant > 0, then the roots are real.
  • If the discriminant = 0, then the roots are equal.
  • If the discriminant < 0, then the roots are imaginary.

Roots of the quadratic equation are obtained using these formulas.

First Root = [-b + (b2 – 4ac)**½] / 2a

Second Root = [-b – (b2 – 4ac)**½] / 2a

So, Without further ado, let’s begin the tutorial.

C Program To Find The Roots of a Quadratic Equation

// C Program To Find The Roots of a Quadratic Equation
#include <stdio.h>
#include <math.h>
int main(){
    float a, b, c, discriminant, root_one, root_two;
    
    // Asking for Input
    printf("Enter the Values of a, b and c\n");
    scanf("%f %f %f", &a, &b, &c);
    
    discriminant = b * b - 4 * a * c;
    
    // logic
    if (discriminant > 0){
        root_one = (-b + sqrt(discriminant)) / (2 * a);
        root_two = (-b - sqrt(discriminant)) / (2 * a);
        printf("The Real Roots are %f and %f", root_one, root_two);
    }
    
    else if (discriminant == 0){
        root_one = root_two = -b / (2 * a);
        printf("The Equal Roots are %f", a);
    }
    
    else{
        printf("The Roots are Imaginary");
    }
    return 0;
}

Output

Enter the Values of a, b and c
6 11 -35
The Real Roots are 1.666667 and -3.500000

How Does This Program Work ?

    float a, b, c, discriminant, root_one, root_two;

In this program, we have declared 6 float variable names as a, b, c, discriminant, root_one and root_two respectively.

    // Asking for Input
    printf("Enter the Values of a, b and c\n");
    scanf("%f %f %f", &a, &b, &c);

Then, the user is asked to enter the values of a, b and c.

   discriminant = b * b - 4 * a * c

The discriminant of the quadratic equation is find out using b2 – 4ac.

    // logic
    if (discriminant > 0){
        root_one = (-b + sqrt(discriminant)) / (2 * a);
        root_two = (-b - sqrt(discriminant)) / (2 * a);
        printf("The Real Roots are %f and %f", root_one, root_two);
    }

Now, comes the logic part of the program. If discriminant > 0, then roots are found using the formula stated above.

Here, the sqrt() function is used to find the square roots of the number.

    else if (discriminant == 0){
        root_one = root_two = -b / (2 * a);
        printf("The Equal Roots are %f", a);
    }

Similarly, if discriminant = 0, then roots will be equal and can be found using -b /(2 * a) formula.

    else{
        printf("The Roots are Imaginary");
    }

And if discriminant < 0, then the roots will be imaginary.

Some of the used terms are as follows:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.  

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.  

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen. 

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

sqrt() – sqrt() function is used to find the square root of a number.

// – Used for Commenting in C. 

Conclusion

I hope after going through this post, you understand how to find the roots of a quadratic equation using C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *