Easy Tutorial
❮ C Function Raise C Function Cos ❯

C Language Example - Quadratic Equation

C Language Examples

Solve the quadratic equation: ax roots.

Enter the values of three real numbers a, b, c, with a not equal to 0.

Example

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

int main()
{
        float a,b,c,x1,x2,d;
        printf("Enter the coefficients of the equation:");
        scanf("%f %f %f",&a,&b,&c);
        if(a!=0)
        {
                d=sqrt(b*b-4*a*c);
                x1=(-b+d)/(2*a);
                x2=(-b-d)/(2*a);
                if(x1&lt;x2) 
                    printf("%0.2f %0.2f\n",x2,x1); 
                else
                    printf("%0.2f %0.2f\n",x1,x2);
        }
        return 0;
}

Running result:

Enter the coefficients of the equation:1 2 1
-1.00 -1.00

C Language Examples

❮ C Function Raise C Function Cos ❯