ISC practical papers › 2027 masterclass › Episode 4
Leap Year & Date Logic
ISC 2027 Computer Science Practical masterclass · Episode 4 of 4 · Vinay Singh
The problem
Accept a day number (1 to 366), a four-digit year and a number of days N (1 to 100). Validate the inputs. Display the date for the day number with its ordinal suffix (for example 5TH MARCH, 2026), then display the date N days later, moving into the next year where needed.
Rules and messages
- If the year is not between 1000 and 9999, print INVALID YEAR.
- If the day number is less than 1, or greater than 365 (366 in a leap year), print INVALID DAY NUMBER.
- If N is less than 1 or greater than 100, print INVALID NUMBER OF DAYS.
Algorithm
Written step by step, the way the ISC practical asks for it, and matched to the program below. How the algorithm is marked
- Step 1: Start.
- Step 2: Input dayNumber, year and n.
- Step 3: If the year is not a four-digit number, display INVALID YEAR and stop.
- Step 4: Check for a leap year using the Gregorian rule: divisible by 400, or divisible by 4 and not by 100. Set maxDays to 366 for a leap year, otherwise 365.
- Step 5: If dayNumber is less than 1 or greater than maxDays, display INVALID DAY NUMBER and stop.
- Step 6: If n is less than 1 or greater than 100, display INVALID NUMBER OF DAYS and stop.
- Step 7: Set up the daysInMonth array, with February set to 29 in a leap year and 28 otherwise.
- Step 8: Find the date for dayNumber by taking off the days of each month in turn.
- Step 9: Compute targetDay = dayNumber + n. If it is more than the days in the year, subtract the year's days and add 1 to the year.
- Step 10: Find the date for targetDay in targetYear.
- Step 11: Display the current date and the projected date, each with its ordinal suffix.
- Step 12: Stop.
Variable description
| Name | Data type | Purpose |
|---|---|---|
dayNumber | int | day of the year entered |
year | int | calendar year entered |
n | int | number of days to add |
isLeap | boolean | true for a leap year |
maxDays | int | number of days in the year (365 or 366) |
currentDate | String | the date for dayNumber, formatted |
targetDay | int | day of the target year after adding n days |
targetYear | int | year of the projected date |
targetMaxDays | int | number of days in targetYear |
futureDate | String | the projected date, formatted |
daysInMonth | int[] | inside getDate(): days in each month (index 1 to 12) |
monthNames | String[] | inside getDate(): month names in upper case |
d, m | int | inside getDate(): day remainder and month index |
Methods in the program
- isLeapYear(int y): returns true when y is divisible by 400, or by 4 and not by 100.
- getSuffix(int d): returns ST, ND, RD or TH, with 11, 12 and 13 always taking TH.
- getDate(int dayNum, int year): returns the date text for a day of the year.
Java solution
import java.util.Scanner;
/**
* ISC Computer Science Practical Examination
* Topic: Date, Time & Number Logic (Day-of-the-year Calculation & Leap Year Century Rule)
* Class: DateCalculation
*/
public class DateCalculation {
// Century Leap Year Law
public static boolean isLeapYear(int y) {
return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
}
// Ordinal suffix helper: 1st, 2nd, 3rd, 4th... 11th, 12th, 13th, 21st, 22nd...
public static String getSuffix(int d) {
if (d >= 11 && d <= 13) {
return "TH";
}
switch (d % 10) {
case 1: return "ST";
case 2: return "ND";
case 3: return "RD";
default: return "TH";
}
}
// Helper method to convert (dayNumber, year) into formatted Date string
public static String getDate(int dayNum, int year) {
int[] daysInMonth = {0, 31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String[] monthNames = {"", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
int d = dayNum;
int m = 1;
while (d > daysInMonth[m]) {
d -= daysInMonth[m];
m++;
}
return d + getSuffix(d) + " " + monthNames[m] + ", " + year;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Accept Inputs
System.out.print("DAY NUMBER: ");
int dayNumber = in.nextInt();
System.out.print("YEAR: ");
int year = in.nextInt();
System.out.print("DATE AFTER (N DAYS): ");
int n = in.nextInt();
// 1. Validate Year
if (year < 1000 || year > 9999) {
System.out.println("INVALID YEAR");
return;
}
// 2. Validate Day Number according to Leap Year status
boolean isLeap = isLeapYear(year);
int maxDays = isLeap ? 366 : 365;
if (dayNumber < 1 || dayNumber > maxDays) {
System.out.println("INVALID DAY NUMBER");
return;
}
// 3. Validate N (1 <= N <= 100)
if (n < 1 || n > 100) {
System.out.println("INVALID NUMBER OF DAYS");
return;
}
// 4. Compute Date corresponding to Day Number
String currentDate = getDate(dayNumber, year);
// 5. Compute Date after N days (handling year boundary crossover)
int targetDay = dayNumber + n;
int targetYear = year;
int targetMaxDays = isLeapYear(targetYear) ? 366 : 365;
if (targetDay > targetMaxDays) {
targetDay -= targetMaxDays;
targetYear++;
}
String futureDate = getDate(targetDay, targetYear);
// Display Outputs
System.out.println("\nOUTPUT:");
System.out.println("DATE: " + currentDate);
System.out.println("DATE AFTER " + n + " DAYS: " + futureDate);
}
}
Sample runs
Each run uses the program above. The output is copied from the terminal.
Day 64 of 2026, add 45 days
Input, typed in this order
64 2026 45
Output
DAY NUMBER: YEAR: DATE AFTER (N DAYS): OUTPUT: DATE: 5TH MARCH, 2026 DATE AFTER 45 DAYS: 19TH APRIL, 2026
Leap year: day 60 of 2000, add 10 days
Input, typed in this order
60 2000 10
Output
DAY NUMBER: YEAR: DATE AFTER (N DAYS): OUTPUT: DATE: 29TH FEBRUARY, 2000 DATE AFTER 10 DAYS: 10TH MARCH, 2000
Invalid: day 366 in 2001 (not a leap year)
Input, typed in this order
366 2001 1
Output
DAY NUMBER: YEAR: DATE AFTER (N DAYS): INVALID DAY NUMBER