Code With Vinay Logo Code with Vinay

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

  1. Step 1: Start.
  2. Step 2: Input dayNumber, year and n.
  3. Step 3: If the year is not a four-digit number, display INVALID YEAR and stop.
  4. 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.
  5. Step 5: If dayNumber is less than 1 or greater than maxDays, display INVALID DAY NUMBER and stop.
  6. Step 6: If n is less than 1 or greater than 100, display INVALID NUMBER OF DAYS and stop.
  7. Step 7: Set up the daysInMonth array, with February set to 29 in a leap year and 28 otherwise.
  8. Step 8: Find the date for dayNumber by taking off the days of each month in turn.
  9. 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.
  10. Step 10: Find the date for targetDay in targetYear.
  11. Step 11: Display the current date and the projected date, each with its ordinal suffix.
  12. Step 12: Stop.

Variable description

NameData typePurpose
dayNumberintday of the year entered
yearintcalendar year entered
nintnumber of days to add
isLeapbooleantrue for a leap year
maxDaysintnumber of days in the year (365 or 366)
currentDateStringthe date for dayNumber, formatted
targetDayintday of the target year after adding n days
targetYearintyear of the projected date
targetMaxDaysintnumber of days in targetYear
futureDateStringthe projected date, formatted
daysInMonthint[]inside getDate(): days in each month (index 1 to 12)
monthNamesString[]inside getDate(): month names in upper case
d, mintinside getDate(): day remainder and month index

Methods in the program

Java solution

DateCalculation.java Download
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