用fortran语言编写:输入某一年年份year,判断它是否为闰年(Leap)。判断依据为:(1)year能被4整除,但又不能被100整除的年份都是闰年;(2)year能被100整除,且能被400整除的年份是闰年。
时间: 2023-10-23 20:16:13 浏览: 43
以下是用Fortran语言编写的程序,可以判断输入的年份是否为闰年:
```
program leap_year
implicit none
integer :: year, flag
write(*,*) "请输入一个年份:"
read(*,*) year
flag = 0
if (mod(year, 4) == 0) then
flag = 1
if (mod(year, 100) == 0) then
flag = 0
if (mod(year, 400) == 0) then
flag = 1
endif
endif
endif
if (flag == 1) then
write(*,*) year, "是闰年"
else
write(*,*) year, "不是闰年"
endif
end program leap_year
```
在程序中,首先通过 `read` 语句获取用户输入的年份值,并将其存储在整型变量 `year` 中。然后,通过 `mod` 函数判断该年份是否能被4整除,如果可以,则将标志变量 `flag` 的值设为1。接着,再判断该年份是否能被100整除,如果可以,则将 `flag` 的值设为0,但如果能同时被400整除,则将 `flag` 的值重新设为1。最后,根据 `flag` 的值输出判断结果。
相关问题
用Fortran语言编写:给定一个数N,判断是否能同时被3和 5整除。
程序如下:
program is_divisible_by_3_and_5
implicit none
integer :: N
write(*,"('Enter an integer: ')", advance="no")
read(*,*) N
if (mod(N,3)==0 .and. mod(N,5)==0) then
write(*,"('The number ',I0,' is divisible by both 3 and 5.')") N
else
write(*,"('The number ',I0,' is not divisible by both 3 and 5.')") N
end if
end program is_divisible_by_3_and_5
怎样用fortran判断一年是否为闰年
可以使用以下代码来判断一年是否为闰年:
```fortran
function is_leap_year(year)
logical :: is_leap_year
integer, intent(in) :: year
is_leap_year = (mod(year, 4) == 0) .and. ((mod(year, 100) /= 0) .or. (mod(year, 400) == 0))
end function is_leap_year
```
其中,`year` 为需要判断的年份,函数返回值为逻辑值,表示该年是否为闰年。