In: Computer Science
What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday
Write a function is leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not.
.Then, write a function days_since() which takes 3 input integers day, month, year, representing a date (after 1st January 1900), and returns the total number of days passed since the 1st January 1900. (For example, days_since(2, 1, 1900) should return 1.
Finally, write a function week_day() which takes 3 input integers day, month, year, representing a date (after 1st January 1900), and prints the day of the week corresponding to the date
Dont use any inbuilt function, use looping.
Function to check Leap year
static boolean isLeap(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
Function to return the total number of days passed since the 1st January 1900
static int monthDays[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
static int countLeapYears(int m,int y)
{
int years = y;
if (m<= 2)
{
years--;
}
return years / 4 - years / 100 + years / 400;
}
static int days_since(int d,int m,int y)
{
int n1 = 1900 * 365 + 1;
for (int i = 0; i < 1 - 1; i++)
{
n1 += monthDays[i];
}
n1 += countLeapYears(1,1900);
int n2 = y * 365 + d;
for (int i = 0; i < m - 1; i++)
{
n2 += monthDays[i];
}
n2 += countLeapYears(m,y);
return (n2 - n1);
}
Function to prints the day of the week corresponding to the date
static String week_day(int d, int m, int y)
{
int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
String d1[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
y -= (m < 3) ? 1 : 0;
return d1[( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7];
}
All the above funcrions are putted in switch case to test
Source Code
import java.util.*;
public class Date
{
static boolean isLeap(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
static int monthDays[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
static int countLeapYears(int m,int y)
{
int years = y;
if (m<= 2)
{
years--;
}
return years / 4 - years / 100 + years / 400;
}
static int days_since(int d,int m,int y)
{
int n1 = 1900 * 365 + 1;
for (int i = 0; i < 1 - 1; i++)
{
n1 += monthDays[i];
}
n1 += countLeapYears(1,1900);
int n2 = y * 365 + d;
for (int i = 0; i < m - 1; i++)
{
n2 += monthDays[i];
}
n2 += countLeapYears(m,y);
return (n2 - n1);
}
static String week_day(int d, int m, int y)
{
int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
String d1[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
y -= (m < 3) ? 1 : 0;
return d1[( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7];
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 to check Leap Year\nEnter 2 to find the total number of days passed since the 1st January 1900\nEnter 3 to to prints the day of the week corresponding to the date");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter year to check");
int year=sc.nextInt();
System.out.println(isLeap(year));break;
case 2: System.out.println("Enter day to check");
int d=sc.nextInt();
System.out.println("Enter month to check");
int m=sc.nextInt();
System.out.println("Enter year to check");
int y=sc.nextInt();
System.out.println("Number of day since 1st january 1990 ="+days_since(d,m,y));break;
case 3: System.out.println("Enter day to check");
int d1=sc.nextInt();
System.out.println("Enter month to check");
int m1=sc.nextInt();
System.out.println("Enter year to check");
int y1=sc.nextInt();
System.out.println("Day of the week is "+week_day(d1,m1,y1));break;
default:System.out.println("Wrong Input");break;
}
}
}
Output of all Functions used above
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
1
Enter year to check
2020
true
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
2
Enter day to check
2
Enter month to check
1
Enter year to check
1990
Number of day since 1st january 1990 =32873
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
2
Enter day to check
2
Enter month to check
1
Enter year to check
1900
Number of day since 1st january 1990 =1
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
3
Enter day to check
13
Enter month to check
10
Enter year to check
2020
Day of the week isTuesday
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
3
Enter day to check
13
Enter month to check
10
Enter year to check
2020
Day of the week is Tuesday