Leap Year Check¶
Concept Explanation¶
What is it?¶
A leap year is a calendar year that contains an additional day compared to a common year. This extra day (February 29th) is added to keep the calendar year synchronized with the astronomical year or seasonal year.
Why is it important?¶
Leap years are essential for maintaining the accuracy of our calendar system over long periods. Without them, our calendar would slowly drift with respect to the seasons. Understanding how to calculate them is a common programming exercise that tests logical reasoning and conditional statements.
Where is it used?¶
- Calendar Applications: All digital calendars need to correctly identify leap years to display correct dates.
- Scheduling Systems: Any system dealing with dates and time, especially over long periods, must account for leap years.
- Historical Data Analysis: Analyzing trends or events over centuries requires accurate date calculations.
Real-world example¶
The Olympic Games typically occur every four years. If a leap year rule is applied, it helps align the games with a consistent interval in the calendar.
Algorithm¶
- Start.
- Get a year (
year) from the user. - Check the conditions for a leap year:
a. If
yearis divisible by 400, it is a leap year. b. Else ifyearis divisible by 100, it is NOT a leap year. c. Else ifyearis divisible by 4, it IS a leap year. d. Else, it is NOT a leap year. - Display the result.
- End.
Leap Year Rules: 1. A year is a leap year if it is divisible by 4. 2. However, if the year is divisible by 100, it is NOT a leap year, unless... 3. The year is also divisible by 400. Then it IS a leap year.
Edge Cases: - Non-integer input (handled by language-specific error mechanisms or explicit validation). - Historical context: The Gregorian calendar rules for leap years were adopted at different times by different countries. This program assumes modern Gregorian calendar rules.
Implementations¶
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
boolean isLeap = false;
if (year % 400 == 0) {
isLeap = true;
} else if (year % 100 == 0) {
isLeap = false;
} else if (year % 4 == 0) {
isLeap = true;
} else {
isLeap = false;
}
if (isLeap)
System.out.println(year + " is a LEAP YEAR.");
else
System.out.println(year + " is NOT a LEAP YEAR.");
scanner.close();
}
}
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
printf("Input Year: %d\n", year);
if (year % 400 == 0) {
printf("%d is a LEAP YEAR.\n", year);
} else if (year % 100 == 0) {
printf("%d is NOT a LEAP YEAR.\n", year);
} else if (year % 4 == 0) {
printf("%d is a LEAP YEAR.\n", year);
} else {
printf("%d is NOT a LEAP YEAR.\n", year);
}
return 0;
}
SET SERVEROUTPUT ON;
DECLARE
input_year NUMBER := &Enter_a_Year;
BEGIN
DBMS_OUTPUT.PUT_LINE('Input Year: ' || input_year);
IF MOD(input_year, 400) = 0 THEN
DBMS_OUTPUT.PUT_LINE(input_year || ' is a LEAP YEAR.');
ELSIF MOD(input_year, 100) = 0 THEN
DBMS_OUTPUT.PUT_LINE(input_year || ' is NOT a LEAP YEAR.');
ELSIF MOD(input_year, 4) = 0 THEN
DBMS_OUTPUT.PUT_LINE(input_year || ' is a LEAP YEAR.');
ELSE
DBMS_OUTPUT.PUT_LINE(input_year || ' is NOT a LEAP YEAR.');
END IF;
END;
/
Explanation¶
- Java: Uses nested
if-else if-elsestatements to apply the leap year rules.Scanneris used for input. - Python: Employs logical operators (
or,and,!=) within a singleif-elif-elsestructure, which is a common Pythonic way to express such conditions concisely. - C: Utilizes nested
if-else if-elsestatements with the modulo operator (%) to implement the rules.scanf()andprintf()handle I/O. - Oracle: Implemented in PL/SQL. Uses
MOD()function andIF-ELSIF-ELSEstructure. Substitution variables (&Enter_a_Year) prompt for user input.
Complexity Analysis¶
- Time Complexity: O(1) - The number of operations is constant, regardless of the year value.
- Space Complexity: O(1) - A fixed number of variables are used.
Flowchart¶
graph TD
A[Start] --> B[Get Year]
B --> C{Year % 400 == 0?}
C -- Yes --> D[Display Leap Year]
C -- No --> E{Year % 100 == 0?}
E -- Yes --> F[Display Not a Leap Year]
E -- No --> G{Year % 4 == 0?}
G -- Yes --> D
G -- No --> F
D --> H[End]
F --> H
Sample Dry Run¶
| Step | Year | Year % 400 | Year % 100 | Year % 4 | isLeap | Description |
|---|---|---|---|---|---|---|
| Input | 2000 | 0 | - | - | true | User enters 2000 |
| Decision | - | 0 == 0? (True) | - | - | true | 2000 is divisible by 400 |
| Output | - | - | - | - | - | Display "2000 is a LEAP YEAR." |
| End | - | - | - | - | - | Program terminates |
| Input | 1900 | 300 | 0 | - | false | User enters 1900 |
| Decision | - | 300 == 0? (False) | 0 == 0? (True) | - | false | 1900 is divisible by 100, not 400 |
| Output | - | - | - | - | - | Display "1900 is NOT a LEAP YEAR." |
| End | - | - | - | - | - | Program terminates |
| Input | 2024 | 24 | 24 | 0 | true | User enters 2024 |
| Decision | - | 24 == 0? (False) | 24 == 0? (False) | 0 == 0? (True) | true | 2024 is divisible by 4, not 100 |
| Output | - | - | - | - | - | Display "2024 is a LEAP YEAR." |
| End | - | - | - | - | - | Program terminates |
Practice Problems¶
Easy¶
- Modify the program to calculate the number of leap years within a given range (e.g., from 2000 to 2050).
Medium¶
- Write a program that takes a date as input (day, month, year) and validates if it's a real date, considering leap years.
Hard¶
- Implement a custom calendar function that can determine the day of the week for any given date, correctly accounting for leap years.
"The only source of knowledge is experience." - Albert Einstein