LoginSignup
1
0

Fortranでうるう年の判定

Last updated at Posted at 2024-05-10

コード

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 はうるう年ではありません。
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0