C 语言实例 - 一元二次方程

C 语言教程 · 2019-02-20 17:12:29

求一元二次方程:ax2+bx+c=0 的根。

输入三个实数a,b,c的值,且a不等于0。

实例

#include <stdio.h> #include <math.h> int main() { float a,b,c,x1,x2,d; printf("输入方程的三个系数:"); 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; }

运行结果:

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

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

Once Upon an Algorithm

Once Upon an Algorithm

Martin Erwig / MIT Press / 2017-9-8 / GBP 22.95

How Hansel and Gretel, Sherlock Holmes, the movie Groundhog Day, Harry Potter, and other familiar stories illustrate the concepts of computing. Picture a computer scientist, staring at a screen and......一起来看看 《Once Upon an Algorithm》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具