Day of Week

 Description:          The following program takes three numbers as input date: month, date, year and returns the week-day corresponding to it.
                           
Primary Inputs:    Three numbers (month m, day d, year yyyy)
Primary Output:    Determine the week-day (sunday, monday etc.) for the input date.
Platform Used:      JDK 1.6 with Notepad.
 

import java.io.*;

class DayOfWeek {
public static void main(String[] args) {
// take input date values
Console con = System.console();
System.out.print("Enter the month: ");
int M = Integer.parseInt(con.readLine());

System.out.print("Enter the Day of the Month: ");
int d = Integer.parseInt(con.readLine());
System.out.print("Enter the year(yyyy): ");
int y = Integer.parseInt(con.readLine());
int m = 0;
if (M >= 3)
m = M - 2;
else
m = M + 10;

int c = ((y - y % 100) / 100) % 10;
y = y % 100;
int A = (13 * m - 1) / 5;
int B = y / 4;
int C = c / 4;
int D = A + B + C + d + y - 2 * (c % 10);
int R = D % 7;

// System.out.println(m+","+c+","+A+","+B+","+C+","+D+","+R);
// For testing purposes only

switch (R) {
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
}
}
}

 

Rate this post

Leave a Reply