C语言一元二次方程的根
题目内容:
编写程序,从键盘输入一个二元一次方程ax^2+bx+c=0的三个参数a、b、c(均为整数),求此方程的实根。如果方程有实根,则输出实根(精确到小数点后2位,较大实根在前),如果没有实根,则输出"方程没有实根"。
输入格式:
a b c
输出格式:
x1 x2
代码如下
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c,d;
double x1,x2;
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0){
printf("方程没有实数根");
}else{
x1=(-b+sqrt(d))/2*a;
x2=(-b-sqrt(d))/2*a;
printf("%.2f %.2f",x1,x2);
}
return 0;
}
运行结果: