• If the year is not divisible by 4, it is a common year • else A leap year is d
ID: 3601431 • Letter: #
Question
• If the year is not divisible by 4, it is a common year • else A leap year is defined as any calendar year which meets the following criteria: If the year is not divisible by 100, it is a leap year else If the year is not divisible by 400, it is a common year Otherwise it is a leap year For your program you will take in three integers from the user: A four digit year The day of the month (between 1 and 31) The month (between 1 and 12, 1 representing January and 12 representing December) Your program will output whether or not the date entered is a valid calendar date. Be sure to divide your program into functions which perform each major task. Hint: There are 30 days in September, April, June and November. If the year is a leap year, there are 29 days in February. If the year is a common year there are 28 days in February. All other months have 31 days.
Explanation / Answer
Hi friend , you have not mentioned about programming language.
Please find my implementation in Java.
import java.util.Scanner;
public class ValidateDate {
public static boolean isLeapYear(int year) {
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter day: ");
int day = sc.nextInt();
System.out.print("Enter month:");
int month = sc.nextInt();
System.out.print("Enter year:");
int year = sc.nextInt();
int monthDayNumber;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDayNumber = 31;
break;
case 4:
case 6:
case 9:
case 11:
monthDayNumber = 30;
break;
case 2:
if(isLeapYear(year))
monthDayNumber = 29;
else
monthDayNumber = 28;
break;
default:
monthDayNumber = -1;
}
if(monthDayNumber == -1 || monthDayNumber < day || day < 1 || year < 1970){
System.out.println(month+"/"+day+"/"+year+" is not valid");
}else{
System.out.println(month+"/"+day+"/"+year+" is valid");
}
}
}
/*
Sample run:
Enter day: 29
Enter month:2
Enter year:2011
2/29/2011 is not valid
Enter day: 30
Enter month:10
Enter year:2015
10/30/2015 is valid
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.