用fortran输入系数a、b和c,并使用内部子程序来求解方程ax2+bx+c=0。注意,根有三种情况,无根,重复根,两个根。
时间: 2024-06-01 11:12:56 浏览: 18
这是一个关于编程的问题,很高兴回答!下面是使用fortran编写的程序,可以输入系数a、b和c,并使用内部子程序来求解方程ax2+bx+c=0:
program quadratic_equation
implicit none
real :: a, b, c, discriminant, x1, x2, root
print *, 'Please enter coefficient a:'
read *, a
print *, 'Please enter coefficient b:'
read *, b
print *, 'Please enter coefficient c:'
read *, c
discriminant = b ** 2 - 4 * a * c
if (discriminant .lt. 0.0) then
print *, 'This quadratic equation has no real roots!'
else if (discriminant == 0.0) then
root = -b / (2 * a)
print *, 'This quadratic equation has one repeated root:'
print *, 'x = ', root
else
x1 = (-b + sqrt(discriminant)) / (2 * a)
x2 = (-b - sqrt(discriminant)) / (2 * a)
print *, 'This quadratic equation has two real roots:'
print *, 'x1 = ', x1
print *, 'x2 = ', x2
end if
contains
function sqrt(x)
implicit none
real, intent(in) :: x
real :: sqrt
sqrt = sqrt(x)
end function sqrt
end program quadratic_equation
如果你还想了解更多关于fortran编程的知识,请问我吧!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)