Find Roots of Quadratic Equation C Program

By | May 18, 2016

C Program To Find Roots of Quadratic Equation

Learn How To Find Roots of Quadratic Equation in C Programming Language using If – Else Block Structure. This Code To Calculate Quadratic Roots of an Equation is without Functions and also has an Output Screen displayed at the bottom of this page.

What is a Quadratic Equation?

The Standard Form of a Quadratic Equation is ax2 + bx + c = 0, where x is a variable entity and a, b, c are constant values which cannot be changed.

Formula For Quadratic Equation

z = −b ± √(b2 − 4ac) / 2a

Conditions For Discriminants

  • If b2 − 4ac is Negative, Two Complex Solutions are possible
  • If b2 − 4ac is Positive, Two Real Solutions are possible
  • If b2 − 4ac = 0, One Real Solution is possible

Also Read: C Program To Remove Vowels From A String

C Program To Find Roots of Quadratic Equation


int main()
    float root1, root2, discriminant;
    float a, b, c;
    printf("\nEnter The Values\n");
    scanf("%f", &a);
    scanf("%f", &b);
    scanf("%f", &c);
    discriminant = b*b - 4*a*c;
    if(discriminant < 0)
        printf("\nRoots Are Imaginary\n");
        root1 = (-b + sqrt(discriminant)) / (2*a);
        root2 = (-b - sqrt(discriminant)) / (2*a);
        printf("\nRoot 1 = %f\n", root1);
        printf("\nRoot 2 = %f\n", root2);
    return 0;

Note: If you Compile this Code in Linux, you will get a Linker Error. It is because the implementation of sqrt() method is missing and it is defined in libm library in GNU GCC Compiler. Therefore, you have to explicitly apply it while compilation.


Command: gcc test.c -lm


Also Read: Find Sum of Lower Triangular Elements of Matrix C Program


C Program To Find Roots of Quadratic Equation using If Else

If you have any compilation errors or doubts in this Program to Find Roots of Quadratic Equation in C Programming Language, let us know about in the Comment Section below.

One thought on “Find Roots of Quadratic Equation C Program

  1. Mahesh Asalkar

    It is really a short and simple code for finding quadratic roots in c language. Thanks.


Let's Discuss