Code with Vinay

ISC practical papers › ISC 2019 › Question 1

Date after 'N' days

ISC 2019 Computer Science Practical, Question 1 · Date programs

The question

Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user to generate and display the corresponding date.

Also, accept ‘N’ (1 <= N <= 100) from the user to compute and display the future date corresponding to ‘N’ days after the generated date.

Display an error message if the value of the day number, year and N are not within the limit or not according to the condition specified.

Test your program with the following data and some random data:

Example 1

INPUT:
DAY NUMBER:             255
YEAR:                     2018
DATE AFTER (N DAYS):     22
OUTPUT:
DATE:                     12 TH SEPTEMBER, 2018
DATE AFTER 22 DAYS:     4 TH OCTOBER, 2018

Example 2

INPUT:
DAY NUMBER:             360
YEAR:                     2018
DATE AFTER (N DAYS):     45
OUTPUT:
DATE:                     26 TH DECEMBER, 2018
DATE AFTER 45 DAYS:     9 TH FEBRUARY, 2019

Example 3

INPUT:
DAY NUMBER:             500
YEAR:                     2018
DATE AFTER (N DAYS):     33
OUTPUT:
DAY NUMBER OUT OF RANGE.

Example 4

INPUT:
DAY NUMBER:             150
YEAR:                     2018
DATE AFTER (N DAYS):     330
OUTPUT:
DATE AFTER (N DAYS) OUT OF RANGE.

Question: ISC 2019 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2019Q1.java Download GitHub
import java.util.*;

class Date {
    private int day, month, year;

    public Date(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public String toString() {
        String suffix[] = { "TH", "ST", "ND", "RD", "TH", "TH", "TH", "TH", "TH", "TH" };
        String monthName[] = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER",
                "OCTOBER", "NOVEMBER", "DECEMBER" };
        String finalSuffix;
        if (day > 9 && day < 21)
            finalSuffix = "TH";
        else
            finalSuffix = suffix[day % 10];
        return day + " " + finalSuffix + " " + monthName[month - 1] + ", " + year;
    }
}

class ISC2019Q1 {
    static int dayNumber, year, N;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT");
        System.out.print("DAY NUMBER:\t\t");
        dayNumber = in.nextInt();
        System.out.print("YEAR:\t\t\t");
        year = in.nextInt();
        System.out.print("DATE AFTER (N DAYS):\t");
        N = in.nextInt();

        System.out.println("OUTPUT");
        if (dayNumber < 1 || dayNumber > 366) {
            System.out.println("DAY NUMBER OUT OF RANGE.");
        } else if (year < 1000 || year > 9999) {
            System.out.println("YEAR OUT OF RANGE.");
        } else if (N < 1 || N > 100) {
            System.out.println("DATE AFTER (N DAYS) OUT OF RANGE.");
        } else {
            Date dt = dateFromDayNumber(dayNumber, year);
            System.out.println("DATE:\t\t\t" + dt);
            int nextDayNumber = dayNumber + N, nextYear;
            if (nextDayNumber > 365) {
                nextYear = year + 1;
            } else {
                nextYear = year;
            }
            if (isLeap(year)) {
                nextDayNumber = nextDayNumber % 366;
            } else {
                nextDayNumber = nextDayNumber % 365;
            }
            Date dateAfterNDays = dateFromDayNumber(nextDayNumber, nextYear);
            System.out.println("DATE AFTER " + N + " DAYS:\t" + dateAfterNDays);
        }
        in.close();
    }

    private static Date dateFromDayNumber(int dayNumber, int year) {
        int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (isLeap(year))
            month[1] = 29;
        int monthNumber;
        for (monthNumber = 0; monthNumber < month.length; monthNumber++) {
            if (dayNumber <= month[monthNumber])
                break;
            dayNumber -= month[monthNumber];
        }
        return new Date(dayNumber, monthNumber + 1, year);
    }

    public static boolean isLeap(int year) {
        // int divisor;
        // if(year%100==0) divisor=400;
        // else divisor=4;
        // return year%divisor==0;
        return year % (year % 100 == 0 ? 400 : 4) == 0;
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers