[RUBY] Program to determine if it is a leap year

Enter the year and month of the Christian era and write a program to find the number of days in that month. In that case, you need to consider the leap year.

Leap years are determined by the following criteria.

① If the year is divisible by 4, it is a leap year. (2) However, as an exception, if the year is divisible by 100, it is not a leap year. ③ However, as an exception, if it is divisible by 400, it is a leap year.

In other words, the year 2000 is a leap year, and the year 2100 is not a leap year.

** Output example ** February 1990 => "February 1990 has 28 days" February 2000 => "February 2000 has 29 days" February 2100 => "February 2100 has 28 days" March 2000 => "March 2000 has 31 days"

** Model answer **

def get_days(year, month)
  month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  if month == 2
    if year % 4 == 0
      if year % 100 == 0 && year % 400 != 0
        days = 28
      else
        days = 29
      end
    else
      days = 28
    end
  else
    days = month_days[month - 1]
  end

  return days
end

puts "Please enter the year:"
year = gets.to_i
puts "Please enter the month:"
month = gets.to_i

days = get_days(year, month)
puts "#{year}Year#{month}The moon#{days}There are days"

** Explanation ** Except for February, the number of days in each month is fixed. Therefore, first write the code as follows.

def get_days(year, month)
  month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] #The number of days in each month is managed by an array
  return month_days[month - 1]
end

puts "Please enter the year:"
year = gets.to_i
puts "Please enter the month:"
month = gets.to_i

days = get_days(year, month)
puts "#{year}Year#{month}The moon#{days}There are days"

Then, only in February, the conditional expression is used for output.

def get_days(year, month)
  month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  if month == 2 #In February
    #Substitute 29 for days in leap years
    #Otherwise, substitute 28 for days
  else
    days = month_days[month - 1]
  end

  return days
end

puts "Please enter the year:"
year = gets.to_i
puts "Please enter the month:"
month = gets.to_i

days = get_days(year, month)
puts "#{year}Year#{month}The moon#{days}There are days"

Determine if it is a leap year. There were three leap year conditions, but they can be summarized as follows.

① That year is divisible by 4 ② However, if the year is divisible by 100 and not divisible by 400, it is not a leap year.

Therefore, the following conditional branches can be provided. [Example]

if year % 4 == 0  #The year is divisible by 4
  if year % 100 == 0 && year % 400 != 0  #If the year is divisible by 100 and not divisible by 400
    #Not a leap year
  else
    #leap year
  end
end

If you reflect the above logic

def get_days(year, month)
  month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  if month == 2
    if year % 4 == 0  #The year is divisible by 4
      if year % 100 == 0 && year % 400 != 0  #If the year is divisible by 100 and not divisible by 400
        days = 28 #Not a leap year
      else
        days = 29 #leap year
      end
    else
      days = 28   #Not a leap year
    end
  else
    days = month_days[month - 1]
  end

  return days
end

puts "Please enter the year:"
year = gets.to_i
puts "Please enter the month:"
month = gets.to_i

days = get_days(year, month)
puts "#{year}Year#{month}The moon#{days}There are days"

Recommended Posts

Program to determine if it is a leap year
Resultset's next () is not a "method to determine if there is a ResultSet next".
Get the type of an array element to determine if it is an array
Resultset's next () was misunderstood as a method to determine if there is a next
[Swift5] How to compare specific times to determine if it is before or after
[Ruby] Leap year judgment program
To write a user-oriented program (1)
If you want to make a Java application a Docker image, it is convenient to use jib.
Use Modifier # isStatic to determine if the [Reflection] method is static.
A program that determines whether the entered integer is close to an integer
I want to summarize Apache Wicket 8 because it is a good idea
Memo: [Java] If a file is in the monitored directory, process it.
[Introduction to Java] How to write a Java program
Is it a loss if you don't know? Rails [Super] 5 selections to find error mistakes to escape from beginners
What to do if you get a SQLite3 :: BusyException: database is locked error
If the parameter is an array, how to include it in Stopara's params.permit
How to check if an instance variable is defined in a Ruby class
[Spring Boot] If you use Spring Boot, it was convenient to use a lot of util.
What to do when is invalid because it does not start with a'-'
The patchForObject added to RestTemplate cannot be used effectively if it is a default implementation (the one that uses HttpURLConnection).
Introduction to Recursive Functions: What is a Recursive Function?
The end of catastrophic programming # 03 "Comparison of integers, if" a> b ", assume that it is" a --b> 0 ""