Drop links or images here to add them to the editor.

Is it a leap year?

★☆☆

In 1582 the Gregorian calendar was introduced. Our planet takes approximately 365.25 days to orbit the sun once. It's that .25 that creates the need for a leap year every four years. During non-leap years (common years) the calendar doesn't take into account the extra quarter of a day required by Earth to complete a single orbit. In leap years there are 29 days in February instead of 28.

Leap year

Make

Write a program to say whether the year entered by a user is a leap year or not. Any year exactly divisible by four is usually a leap year unless it is also divisible by one hundred. The exception is that all years divisible by four hundred are always leap years.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called is_leap_year that:

  1. Returns true if the year is a leap year or false if it is not.
  2. If the year is exactly divisible by four it is a leap year…
  3. …unless it is divisible by one hundred in which case it is not a leap year…
  4. …although any year that is divisible by four hundred is a leap year.

Complete the main program so that:

  1. The user can input a year.
  2. It outputs whether the year is a leap year or not.

Typical inputs and outputs from the program would be:

Enter a year:
1900
1900 is not a leap year.
Enter a year:
2000
2000 is a leap year.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Leap year program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Function to return if a year is a leap year
    static bool is_leap_year(int year)
    {
---
        bool leap_year = false;
        // If the year is divisible by 4 it is a leap year...
        if (year % 4 == 0)
        {
---
            leap_year = true;
            //  ...however, if it is also divisible by 100 it is not a leap year
            if (year % 100 == 0)
            {
---
                //...but if it is divisible by 400 then it is a leap year
                if (year % 400 == 0)
                {
---
                    leap_year = true;
---
                }
---
                // All other years are not leap years
                else
                {
---
                    leap_year = false;
---
                }
---
            }
---
            else
            {
---
                leap_year = true;
---
            }
---
        }
---
        else
        {
---
            leap_year = false;
---
        }
---
        return leap_year;
---
    }
---


    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter a year:");
        int year = Convert.ToInt32(Console.ReadLine());
---
        if (is_leap_year(year))
        {
---
            Console.WriteLine($"{year} is a leap year.");
---
        }
---
        else
        {
---
            Console.WriteLine($"{year} is not a leap year.");
---
        }
---
    }
---
}