Program opened below. You can scroll through the algorithm, Java code and output.
Program 60
Day Number to Date and Future Date
Question
Accept a day number (1–365 or 366, depending on leap year), a four-digit year and N (1–100). Display the date represented by the day number and the date N days later. Reject values outside the stated limits.
Algorithm
Step 1: Start.
Step 2: Declare the variables and data structures required for the task.
Step 3: Input dayNo, year and N.
Step 4: Check the year and verify that dayNo is within 1..365 or 1..366 for a leap year; also require 1 <= N <= 100.
Step 5: Initialize the counters, flags, accumulators or result structures needed by the solution.
Step 6: If any value is invalid, display INVALID INPUT and stop.
Step 7: Create the first date as January 1 of year plus dayNo - 1 days.
Step 8: Create the future date by adding N days to the first date.
Step 9: Store the computed result and prepare it for display.
Step 10: Display both dates using day, month and year.
Step 11: Stop.
Java program
Javaimport java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
class DayNumberDate {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dayNo = sc.nextInt();
int year = sc.nextInt();
int n = sc.nextInt();
boolean leap = Year.isLeap(year);
if (year < 1000 || year > 9999 || dayNo < 1 || dayNo > (leap ? 366 : 365) || n < 1 || n > 100) {
System.out.println("INVALID INPUT");
return;
}
LocalDate first = LocalDate.of(year, 1, 1).plusDays(dayNo - 1L);
LocalDate future = first.plusDays(n);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("d MMMM uuuu", Locale.ENGLISH);
System.out.println(first.format(fmt).toUpperCase(Locale.ROOT));
System.out.println("DATE AFTER " + n + " DAYS: " + future.format(fmt).toUpperCase(Locale.ROOT));
}
}Sample output
Input: 233 2020 17
Output:
20 AUGUST 2020
DATE AFTER 17 DAYS: 6 SEPTEMBER 2020