Answer:
Here is the program:
import java.util.Scanner; // to accept input from user
public class Main { //class definition
public static void main(String[] args) { //start of main function
Scanner input = new Scanner(System.in); //creates Scanner class object
System.out.print("Enter year: "); //prompts user to enter Year
int year = input.nextInt(); // reads input year from user
System.out.print("Enter month: "); //prompts user to enter month
int month = input.nextInt(); // reads input month from user
System.out.print("Enter the day of the month: "); //prompts user to enter day of month
int dayM = input.nextInt(); // reads input day of month from user
if (month == 1 || month == 2){ //if month is January or February
month = (month == 1) ? 13 : 14; //if month is January set month to 13 and 14 otherwise
year--; } //decrements value of year
int dayW = (dayM + (26 * (month + 1)) / 10 + (year % 100)
+ (year % 100) / 4 + (year / 100) / 4 + 5 * (year / 100)) % 7; //computes day of the week
System.out.print("Day of the week is "); //prints day of week
switch(dayW) { //used to print day of the week
case 0: System.out.println("Saturday"); break; //if 0 then displays Saturday
case 1: System.out.println("Sunday"); break; //1 then displays Sunday
case 2: System.out.println("Monday"); break; //2 then displays Monday
case 3: System.out.println("Tuesday"); break; //3 then displays Tuesday
case 4: System.out.println("Wednesday"); break; //4 then displays Wednesday
case 5: System.out.println("Thursday"); break; //5 then displays Thursday
case 6: System.out.println("Friday"); } } } //6 then displays Friday
Step-by-step explanation:
The program first prompts the user to enter the values of year, month and day of month. Then uses the following formula to compute day of the week in that particular year, month and day of week:
Suppose
year = 2020
month = 9
day of month = dayM = 6
Then dayM is calculated as
(dayM + (26 * (month + 1)) / 10 + (year % 100)
+ (year % 100) / 4 + (year / 100) / 4 + 5 * (year / 100)) % 7
(6+ (26 * (9+ 1)) / 10 + (2020% 100)
+ (2020% 100) / 4 + (2020/ 100) / 4 + 5 * (2020/ 100)) % 7;
This gives answer as 1.
Now if we see the switch statement the day corresponding to 1 is Sunday
So the day on year 2020, month 9 and day of the month 6 is Sunday.
The screenshot of program along with its output is attached.