!------------------------------------------------------------------------------
!
!  Program name:
!
!    no_days
!
!  Purpose:
!
!    This program reads in a month number (between 1 and 12) and a
!    year number and uses the CASE construct to assign the number 
!    of days for that month, taking leap years into account.
!
!------------------------------------------------------------------------------

PROGRAM no_days

  IMPLICIT NONE

  INTEGER :: month, no_of_days, year
  LOGICAL :: leap_year, valid_month

  WRITE(*,*) 'Please enter month number and year'

  ! Read month number and year from terminal:
  READ (*,*) month, year

  ! Test for leap year:
  leap_year = MOD(year, 4) == 0

  ! Assign number of days via CASE construct:
  SELECT CASE (month)

    CASE (9, 4, 6, 11)          ! 30 days hath September, April, June and November
      no_of_days = 30

    CASE (1, 3, 5, 7:8, 10, 12) ! All the rest have 31 except February
      no_of_days = 31

    CASE (2)                    ! February depends on leap year:
      For_February: SELECT CASE (leap_year)

        CASE (.FALSE.)
          no_of_days = 28
        CASE (.TRUE.)
          no_of_days = 29

      END SELECT For_February

    CASE DEFAULT                ! Any other number not valid
      valid_month = .FALSE.
      WRITE(*,*) month, ' is not the number of a month'

  END SELECT

  IF (valid_month) WRITE (*,*) 'Number of days in month number ', &
                                month, ', year ', year, ' is ', no_of_days

END PROGRAM no_days
