Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Program to Check Leap Year

Posted in C# Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 259 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C# Program to Check Leap Year

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, determining whether a given year is a leap year is a common requirement. A leap year is a year that is evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year. Checking for leap years involves implementing specific rules to validate the year.

In this tutorial, we will explore a C# program designed to check whether a given year is a leap year.

πŸ“„ Example

Let's delve into the C# code that accomplishes this task.

LeapYearChecker.cs
Copied
Copy To Clipboard
using System;

class LeapYearChecker {
  // Function to check if a year is a leap year
  static bool IsLeapYear(int year) {
    // Leap year conditions
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  }

  // Driver program
  static void Main() {
    // Replace this value with the year you want to check
    int year = 2024;

    // Call the function to check if the year is a leap year
    if (IsLeapYear(year))
      Console.WriteLine($"{year} is a leap year.");
    else
      Console.WriteLine($"{year} is not a leap year.");
  }
}

πŸ’» Testing the Program

To test the program with different years, modify the value of year in the Main method.

Output
2024 is a leap year.

Compile and run the program to check if the specified year is a leap year.

🧠 How the Program Works

  1. The program defines a class LeapYearChecker containing a static method IsLeapYear that takes an integer year as input and returns true if the year is a leap year, and false otherwise.
  2. The method checks leap year conditions: the year must be divisible by 4 and not divisible by 100, or it must be divisible by 400.
  3. Inside the Main method, replace the value of year with the desired year you want to check.
  4. The program calls the IsLeapYear method and prints the result.

πŸ“ Between the Given Range

Let's dive into the C# code that checks for leap years in the specified range.

LeapYearChecker.cs
Copied
Copy To Clipboard
using System;

class LeapYearChecker {
  static void Main() {
    // Define the range
    int startYear = 2024;
    int endYear = 2050;

    // Output the range
    Console.WriteLine($"Leap Years in the Range {startYear} to {endYear}:");

    // Check and output leap years
    for (int year = startYear; year <= endYear; year++) {
      if (IsLeapYear(year)) {
        Console.Write($"{year} ");
      }
    }

    Console.WriteLine();
  }

  // Function to check if a year is a leap year
  static bool IsLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  }
}

πŸ’» Testing the Program

The provided code is already set to check for leap years in the range from 2024 to 2050.

Output
Leap years in the range 2024 to 2050:
2024 2028 2032 2036 2040 2044 2048

Compile and run the program to see the leap years within the range from 2024 to 2050.

🧠 How the Program Works

  1. The program defines a class LeapYearChecker containing the Main method.
  2. It specifies the range from 2024 to 2050.
  3. It outputs the range and checks each year within the range using the IsLeapYear function.
  4. The IsLeapYear function checks if a given year is a leap year based on the leap year rule.
  5. Leap years within the specified range are printed.

🧐 Understanding the Concept of Leap Year

Before delving into the code, let's understand the concept of leap years. Leap years are years that are evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year.

🎒 Optimizing the Program

While the provided program is effective, consider exploring and implementing alternative approaches or optimizations for checking leap years.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C# Program to Check Leap Year), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy