Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Day of the Year

Updated 21 Oct 2026

The day number of a date is its position in the year: 1 January is day 1 and 31 December is day 365, or day 366 in a leap year. This program reads a date as day, month and year, rejects an impossible date, and otherwise prints the day number. This is the Main version: everything is written in the main method.

Definition

The day number of a date is the number of days from 1 January of that year up to and including that date. It equals the total days in all the complete months before the given month, plus the day of the month.

A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. A leap year has 29 days in February; other years have 28.

  • 1 March 2023: January (31) + February (28) + 1 = $60$.
  • 1 March 2024: 2024 is a leap year, so $31 + 29 + 1 = 61$.
  • 2100 is divisible by 100 but not by 400, so it is not a leap year and 29 February 2100 is invalid. 2000 is divisible by 400, so it is a leap year.

Algorithm

  1. Read the day dd, the month mm and the year yyyy.
  2. Create the array days with the number of days of January to December, taking February as 28 for now.
  3. Find whether the year is a leap year: it is divisible by 4 and not by 100, or divisible by 400. In a leap year set days[1] = 29.
  4. Check the date, in this order: the year must be from 1 to 9999, the month from 1 to 12, and only then the day from 1 to days[mm - 1]. If any test fails, ok is false and the program prints INVALID DATE.
  5. If the date is valid, add the days of all the months before the given month (i from 0 to mm - 2) to total, then add dd.
  6. Print the day number total.

Program

Everything is written inside main: the leap-year test, the check of the date, and the for loop that adds the days of the earlier months.

import java.util.Scanner;
public class DayOfYear
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter day (dd): ");
        int dd = sc.nextInt();                 // day of the date entered
        System.out.print("Enter month (mm): ");
        int mm = sc.nextInt();                 // month of the date entered
        System.out.print("Enter year (yyyy): ");
        int yyyy = sc.nextInt();               // year of the date entered
        int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   // days[0..11] = days in January..December (February = 28 for now)
        boolean leap = (yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0);
        if (leap)
            days[1] = 29;
        else
            days[1] = 28;
        boolean ok = true;                     // true if the date entered is valid
        if (yyyy < 1 || yyyy > 9999)           // year must be 1 to 9999
            ok = false;
        else if (mm < 1 || mm > 12)            // month must be 1 to 12
            ok = false;
        else if (dd < 1 || dd > days[mm - 1])  // day must fit the month (checked only after mm is valid)
            ok = false;
        if (ok)
        {
            int total = 0;                     // day number of the date within its year
            for (int i = 0; i < mm - 1; i++)   // months before the given month
            {
                total = total + days[i];
            }
            total = total + dd;
            System.out.println("Day number of the year: " + total);
        }
        else
            System.out.println("INVALID DATE");
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
ddintDay of the date entered
mmintMonth of the date entered
yyyyintYear of the date entered
daysint[]days[0] to days[11] hold the number of days in January to December
leapbooleanTrue if the year is a leap year
okbooleanTrue if the date entered is valid
totalintDay number of the date within its year
iintLoop counter over the months before the given month

Sample input and output

Sample 1

Enter day (dd): 1
Enter month (mm): 3
Enter year (yyyy): 2024
Day number of the year: 61

Sample 2

Enter day (dd): 31
Enter month (mm): 12
Enter year (yyyy): 2023
Day number of the year: 365

Sample 3

Enter day (dd): 29
Enter month (mm): 2
Enter year (yyyy): 2023
INVALID DATE

One value per line

Dry run

Take the input 1, 3, 2024 (1 March 2024). The program reads dd = 1, mm = 3, yyyy = 2024. In the check, $2024 \bmod 4 = 0$ and $2024 \bmod 100 \ne 0$, so leap is true and days[1] becomes 29. The year, month and day tests all pass, so ok is true.

The loop runs with mm - 1 = 2, so i takes the values 0 and 1:

Stepidays[i]total
Start--0
Loop03131
Loop12960
After loop: add dd--61

The program prints the day number 61, as in the first sample run.

Variations

  1. Date Plus N Days "Read a date and a number N and print the date after N days."
  2. Days left in the year "Print the number of days remaining in the year after the given date."
  3. Date from day number "Read a day number and a year and print the date in dd-mm-yyyy form."