Skip to content

Instantly share code, notes, and snippets.

@imprakharshukla
Created February 6, 2020 14:45
Show Gist options
  • Save imprakharshukla/0119846a26f9de2e14e699a57b80f928 to your computer and use it in GitHub Desktop.
Save imprakharshukla/0119846a26f9de2e14e699a57b80f928 to your computer and use it in GitHub Desktop.
Question 1 of 2019 ISC Practicals
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.Buffer;
public class Q1 {
static int day, N, y, l, k;
static int days[];
static String months[];
public static void main(String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
days = new int[]{31, 29, 31, 30, 30, 31, 30, 31, 31, 30, 31, 30, 31};
months = new String[]{"January", "Feb", "March", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec",};
System.out.println("Enter the day number");
day = Integer.parseInt(br.readLine());
System.out.println("Enter the year");
y = Integer.parseInt(br.readLine());
System.out.println("Enter N");
N = Integer.parseInt(br.readLine());
//Proceeding to check for the given constraints
if (check()) {
//Handling the first date
closestMonth(day);
System.out.println((day - k) + getPrefix(day - k) + " " + months[l] + " , " + y);
//Handling the next dates
if (N + day >= 366) {
//We should now move to another year
int diff = 366 - day;
N = N - diff;
++y;
closestMonth(N);
System.out.println((N - k)+1 + getPrefix(N - k) + " " + months[l] + " , " + y);
} else {
int temp = N + day;
closestMonth(temp);
System.out.println((temp - k)+1 + getPrefix(temp - k) + " " + months[l] + " , " + y);
}
// Manipulating the code to include the difference of the date in the previous run for $day and add that to N
}
}
static boolean check() {
return day <= 366 && y <= 9999 && N <= 100 && N >= 1;
}
static void closestMonth(int u) {
int n = 0;
for (int i = 0; i < days.length; ++i) {
n = n + days[i];
if (n > u) {
l = i;
k = n - days[i];
break;
}
}
}
static String getPrefix(int p) {
if (p == 21 || p == 1)
return "st";
else if (p == 22 || p == 2)
return "nd";
else if (p == 23 || p == 3)
return "rd";
else
return "th";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment