C Language Example - Quadratic Equation

C 语言实例C Language Examples

Solve the quadratic equation:ax2+bx+c=0the 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("Input the three 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<x2) printf("%0.2f %0.2f\n",x2,x1); else printf("%0.2f %0.2f\n",x1,x2); } return 0; }

Run result:

输入方程的三个系数:1 2 1
-1.00 -1.00

C Language Examples

other extensions