Leap Year Determination in C#

This C# program determines whether a given year is a leap year or not. It checks the conditions for leap years, which include being divisible by 4, but not by 100 unless also divisible by 400.

Source Code 💻

using System;

class Program
{
    static void Main()
    {
        // Prompt user to enter a year
        Console.Write("Enter a year: ");
        int year = Convert.ToInt32(Console.ReadLine());

        // Check if the year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            Console.WriteLine($"{year} is a leap year.");
        }
        else
        {
            Console.WriteLine($"{year} is not a leap year.");
        }
    }
}

Explanation

  • The user is prompted to enter a year.
  • The program checks whether the year is a leap year using the following rules:
    • A year is a leap year if it is divisible by 4 and not divisible by 100 unless it is also divisible by 400.
  • Based on the conditions, it prints whether the year is a leap year or not.

Example

  • Input: 2024

  • Output: 2024 is a leap year.

  • Input: 2023

  • Output: 2023 is not a leap year.


🔚 end of document 🔚