コード
function isLeapYear(year) result(bool)
use, intrinsic :: iso_fortran_env
implicit none
integer(int32), intent(in) :: year
logical :: bool
if ((mod(year, 100) == 0) .and. (mod(year, 400) /= 0)) then
bool = .false.
return
end if
if (mod(year, 4) == 0) then
bool = .true.
return
end if
bool = .false.
end function isLeapYear
使い方
program LeapYear
use, intrinsic :: iso_fortran_env
implicit none
integer(int32) :: year_i, year_n
integer(int32), parameter :: yearList(4) = [2001, 2004, 2000, 1900]
do year_i = 1, size(yearList)
year_n = yearList(year_i)
if (isLeapYear(year_n)) then
print *, year_n, "はうるう年です。"
else
print *, year_n, "はうるう年ではありません。"
end if
end do
contains
function isLeapYear(year) result(bool)
integer(int32), intent(in) :: year
logical :: bool
if ((mod(year, 100) == 0) .and. (mod(year, 400) /= 0)) then
bool = .false.
return
end if
if (mod(year, 4) == 0) then
bool = .true.
return
end if
bool = .false.
end function isLeapYear
end program LeapYear
出力
2001 はうるう年ではありません。
2004 はうるう年です。
2000 はうるう年です。
1900 はうるう年ではありません。