Saturday, 25 March 2017

Program For Hundred Year Calender


Step1 - Firstly, select a year and then we need to find out the respective alphabet associated with it (Suppose we are selecting a year '2010' then the alphabet we would be getting is 'E')

Step2 - Secondly, the alphabet obtained from phase1 would act as your row index and the month you are looking would be your column index (now selecting month as 'march' then the column we are looking would be column3 and the row we are looking would be row5 as alphabet 'E' is at 5th row now the output of phase2 would be the intersection of column3 and row5 i.e. '1')

Step3 - Lastly, the number obtained from phase2 would act as a column index and then we need to traverse that column for the date we are looking for (the output from phase2 is '1' so we would be looking column1 and we are selecting date as '10' then we need to traverse column1 for date 10 and the day we would be getting would be 'Wednesday' i.e. final result)



Program -
import java.util.*;
class HundredYearCalender{
public static void main(String sap[]){
Scanner sc = new Scanner(System.in);
int i;
StringBuilder final_date = new StringBuilder();

/*
Phase1 - For phase input would be year and output would be the alphabet associated with that             year
*/
char alpha_28[] =                 {'A','B','C','K','F','G','A','I','D','E','F','N','B','C','D','L','G','A','B','J','E','F','G','H','C','D','E','M'};

//array for 100 years alphabets
char years[] = new char[100];
//variable 'k' keeps track of array alpha_28
int k = 0;
for(i=1;i<100;i++){
years[i] = alpha_28[k];
k++;
k = k % 28;
}

//taking input year
System.out.print("\nEnter the year(2001-2099): ");
int input_year = sc.nextInt();
final_date = final_date.append(input_year + "-");
input_year = input_year - 2000;

//setting the alphabet w.r.t specific year
char final_year = years[input_year];

/*
Phase2 - input would be the alphabet obtained from phase1 and the month you are searching                for and output would be the intersecting number
*/
//setting column values
int months[] = {1,4,4,7,2,5,7,3,6,1,4,6};

/*here subtracting the alphabet getting from phase1 with 64 to get the respective row to opt for*/
int row = (int)final_year - 64;

//taking input month
System.out.print("\nEnter the month: ");
int input_month = sc.nextInt();
final_date = final_date.append(input_month + "-");

input_month = input_month-1;
int initial_value = months[input_month];

int final_month=0;
for(i=0;i<row;i++){
final_month = initial_value;
initial_value++;
initial_value = initial_value % 7;
}

/*
Phase3 - number obtained from phase2 and the date would be input for phase3 and the output              would be the day we are looking for
*/
String weekdays[] =       {"Monday","Tuesday","Wednesday","Thrusday","Friday","Saturday","Sunday"};
System.out.print("\nEnter date: ");
int input_date = sc.nextInt();
final_date = final_date.append(input_date + "");

input_date = input_date + final_month - 1;
int result = -1;
for(i=0;i<input_date;i++){
result++;
result = result % 7;
}

//final result
System.out.print("\nDate(YYYY-MM-DD): "+ final_date + "\nDay: " + weekdays[result]);
}
}

Output -

Enter the year(2001-2099): 2017

Enter the month: 03

Enter date: 25

Date(YYYY-MM-DD): 2017-3-25
Day: Saturday

No comments:

Post a Comment