Menu Close

Check Leap Year Program – FORTRAN

In this program, we have a main program LeapYearCheck and a helper function is_leap_year that determines whether a given year is a leap year.

  • The program prompts the user to enter a year, reads the input, and then calls the is_leap_year function with the year as an argument. The returned logical value indicating whether it’s a leap year or not is stored in the variable is_Leap_Year.
  • The is_Leap_Year function takes the year as an input argument. It checks if the year is divisible evenly by 400, in which case it is a leap year. If not, it checks if the year is divisible evenly by 100.
  • If it is, it’s not a leap year. Finally, if the year is not divisible evenly by 400 or 100, it checks if it’s divisible evenly by 4. If it is, it’s a leap year. Otherwise, it’s not a leap year.
  • The program then displays the result based on the value of is_Leap_Year.

 

program LeapYearCheck
  implicit none
  
  integer :: year
  
  ! Prompt the user for input
  write(*, *) "Enter a year: "
  read(*, *) year
  
  ! Check if the year is a leap year
  if (is_leap_year(year)) then
    write(*, *) year, "is a leap year."
  else
    write(*, *) year, "is not a leap year."
  end if
  
contains

  ! Function to check if a year is a leap year
  logical function is_leap_year(year)
    integer, intent(in) :: year
    
    if (mod(year, 4) == 0) then
      if (mod(year, 100) /= 0 .or. mod(year, 400) == 0) then
        is_leap_year = .true.
        return
      end if
    end if
    
    is_leap_year = .false.
  end function is_leap_year

end program LeapYearCheck

More Related Stuff