Given a year year
and a month month
, return the number of days of that month.
Example 1:
Input: year = 1992, month = 7 Output: 31
Example 2:
Input: year = 2000, month = 2 Output: 29
Example 3:
Input: year = 1900, month = 2 Output: 28
Constraints:
1583 <= year <= 2100
1 <= month <= 12
program main
implicit none
integer :: year, month, days
! Example 1
year = 1992
month = 7
days = get_days(year, month)
print '(A, I0, A, I0, A, I0)', 'Year ', year, ' Month ', month, &
' has ', days, ' days.'
! Example 2
year = 2000
month = 2
days = get_days(year, month)
print '(A, I0, A, I0, A, I0)', 'Year ', year, ' Month ', month, &
' has ', days, ' days.'
! Example 3
year = 1900
month = 2
days = get_days(year, month)
print '(A, I0, A, I0, A, I0)', 'Year ', year, ' Month ', month, &
' has ', days, ' days.'
contains
function get_days(year, month) result(days)
implicit none
integer, intent(in) :: year, month
integer :: days
select case (month)
case (1, 3, 5, 7, 8, 10, 12)
days = 31
case (4, 6, 9, 11)
days = 30
case (2)
if (mod(year, 4) == 0) then
days = 29
else
days = 28
end if
end select
end function get_days
end program main
Year 1992 Month 7 has 31 days. Year 2000 Month 2 has 29 days. Year 1900 Month 2 has 29 days.
! This is a full Fortran script with a valid main entry point that solves the problem
! of finding the number of days in a given month.
! Input:
! year: the year
! month: the month
! Output:
! the number of days in the month
integer, parameter :: year = 1992
integer, parameter :: month = 7
integer :: days
! Check if the year is a leap year
if (mod(year, 4) == 0) then
! If the year is a leap year, check if the month is February
if (month == 2) then
! If the month is February, return 29
days = 29
else
! If the month is not February, return 31
days = 31
end if
else
! If the year is not a leap year, return 31
days = 31
end if
! Print the number of days in the month
write (*, '(A, I0, A, I0, A)') 'The number of days in month ', month, ' of year ', year, ' is ', days
end program
The number of days in month 7 of year 1992 is
def number_of_days(year, month):
if month == 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return 29
return 28
if month in [4, 6, 9, 11]:
return 30
return 31
The algorithm first checks if the given month is February. If it is, then it determines if the given year is a leap year. A leap year is a year that is divisible by 4 and not divisible by 100, or divisible by 400. If it is a leap year, it returns 29 as the number of days in February; otherwise, it returns 28.
If the given month is April, June, September, or November, the algorithm returns 30 as the number of days in that month. For all other months (January, March, May, July, August, October, and December), the algorithm returns 31 as the number of days in that month.
int numberOfDays(int year, int month) {
if(month == 2) {
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 29;
return 28;
}
if(month == 4 || month == 6 || month == 9 || month == 11)
return 30;
return 31;
}
The algorithm first checks if the given month is February. If it is, then it determines if the given year is a leap year. A leap year is a year that is divisible by 4 and not divisible by 100, or divisible by 400. If it is a leap year, it returns 29 as the number of days in February; otherwise, it returns 28.
If the given month is April, June, September, or November, the algorithm returns 30 as the number of days in that month. For all other months (January, March, May, July, August, October, and December), the algorithm returns 31 as the number of days in that month.