PowerShell Script to Check Leap Year
Use the PowerShell code
if (($year % 4 -eq 0) -and (($year % 100 -ne 0) -or ($year % 400 -eq 0))) { 'Leap Year' } else { 'Not Leap Year' } to check if a year is a leap year.Examples
Input2000
OutputLeap Year
Input1900
OutputNot Leap Year
Input2024
OutputLeap Year
How to Think About It
To check if a year is leap, first see if it divides evenly by 4. If yes, then check if it is not divisible by 100 unless it is also divisible by 400. This ensures correct leap year rules.
Algorithm
1
Get the input year.2
Check if the year is divisible by 4.3
If divisible by 4, check if it is not divisible by 100 or divisible by 400.4
If conditions hold, return 'Leap Year', else return 'Not Leap Year'.Code
powershell
param([int]$year) if (($year % 4 -eq 0) -and (($year % 100 -ne 0) -or ($year % 400 -eq 0))) { Write-Output 'Leap Year' } else { Write-Output 'Not Leap Year' }
Output
Leap Year
Dry Run
Let's trace the year 1900 through the code
1
Check divisibility by 4
1900 % 4 = 0 (true)
2
Check divisibility by 100
1900 % 100 = 0 (true)
3
Check divisibility by 400
1900 % 400 = 300 (false)
4
Final decision
Since divisible by 100 but not by 400, result is 'Not Leap Year'
| Year | Divisible by 4 | Divisible by 100 | Divisible by 400 | Leap Year? |
|---|---|---|---|---|
| 1900 | Yes | Yes | No | No |
Why This Works
Step 1: Divisible by 4
A leap year must be divisible by 4, so we check year % 4 -eq 0.
Step 2: Not divisible by 100 unless divisible by 400
Years divisible by 100 are not leap years unless also divisible by 400, checked by ($year % 100 -ne 0) -or ($year % 400 -eq 0).
Alternative Approaches
Using DateTime TryParseExact
powershell
param([int]$year) try { $date = [datetime]::ParseExact("29/02/$year", 'dd/MM/yyyy', $null) Write-Output 'Leap Year' } catch { Write-Output 'Not Leap Year' }
This method tries to create Feb 29 date; if it fails, year is not leap. It is less efficient but uses built-in date parsing.
Using switch statement
powershell
param([int]$year) switch ($true) { ($year % 400 -eq 0) { Write-Output 'Leap Year'; break } ($year % 100 -eq 0) { Write-Output 'Not Leap Year'; break } ($year % 4 -eq 0) { Write-Output 'Leap Year'; break } default { Write-Output 'Not Leap Year' } }
This approach uses switch for clearer logic flow but is longer.
Complexity: O(1) time, O(1) space
Time Complexity
The script performs a fixed number of arithmetic operations and comparisons, so it runs in constant time.
Space Complexity
Only a few variables are used, so space usage is constant.
Which Approach is Fastest?
The modulo arithmetic method is fastest and simplest; using DateTime parsing is slower but leverages built-in validation.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Modulo Arithmetic | O(1) | O(1) | Simple and fast leap year check |
| DateTime Parsing | O(1) | O(1) | Using built-in date validation, less efficient |
| Switch Statement | O(1) | O(1) | Clear logic flow, slightly longer code |
Always test edge years like 1900 and 2000 to verify leap year logic.
Forgetting that years divisible by 100 are not leap years unless divisible by 400.