Tutorialspoint
Problem
Solution
Submissions

if a Year is a Leap Year

Certification: Basic Level Accuracy: 40% Submissions: 5 Points: 5

Write a C# program to implement the IsLeapYear(int year) function, which determines whether a given year is a leap year according to the Gregorian calendar rules.

Algorithm
  • Step 1: A year is a leap year if it is divisible by 4.
  • Step 2: However, if the year is divisible by 100, it is NOT a leap year, unless:
  • Step 3: The year is also divisible by 400, in which case it IS a leap year.
Example 1
  • Input: year = 2000
  • Output: true
  • Explanation:
    • 2000 is divisible by 4 (2000 ÷ 4 = 500 with remainder 0).
    • 2000 is divisible by 100 (2000 ÷ 100 = 20 with remainder 0).
    • 2000 is divisible by 400 (2000 ÷ 400 = 5 with remainder 0).
    • According to leap year rules, since it's divisible by 400, it is a leap year.
Example 2
  • Input: year = 1900
  • Output: false
  • Explanation:
    • 1900 is divisible by 4 (1900 ÷ 4 = 475 with remainder 0).
    • 1900 is divisible by 100 (1900 ÷ 100 = 19 with remainder 0).
    • 1900 is NOT divisible by 400 (1900 ÷ 400 = 4 with remainder 300).
    • According to leap year rules, since it's divisible by 100 but not by 400, it is NOT a leap year.
Constraints
  • 1 ≤ year ≤ 9999
  • The input will be a positive integer representing a year in the Gregorian calendar.
  • Time Complexity: O(1)
  • Space Complexity: O(1)
NumberCognizantZomato
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use the modulo operator (%) to check divisibility.
  • Remember that a year divisible by 4 might not be a leap year if it's divisible by 100.
  • But if it's divisible by 400, it's always a leap year regardless of other rules.
  • Consider the order of your conditional checks for clarity.


Submitted Code :