Write a program that prompts user to input a date and reads
Write a program that prompts user to input a date and reads the date from the keyboard and then tests whether it is a valid date.
The input date will have the format mm dd yyyy separated by spaces or tabs.
If it is a valid date, display the date in the format like: March 20, 2016
If it is not valid, display a message explaining why it is not valid, such as Invalid month value, Invalid day value, etc.
A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be from 1 to a value that is appropriate for the given month. September, April, June, and November each have 30 days. February has 28 days except for leap years when it has 29. The remaining months all have 31 days each. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.
Example Run: (blue background means user input)
Please input a date in mm dd yyyy format separated by space or tab
Date: 03 20 2016
Valid date: March 20, 2016
Example Run:
Please input a date in mm dd yyyy format separated by space or tab
Date: 02 29 2015
Invalid date value.
Example Run:
Please input a date in mm dd yyyy format separated by space or tab
Date: 14 16 2000
Invalid month value.
Requirements:
Produce correct results as stated in requirements.
Display the output as shown in example run.
Write the problem solving algorithm in the beginning of the program (using comments). Additional comments within program will be helpful, but not replacing the algorithm.
Solution
#include <stdio.h>
const char * getMonth(int month)
{
if(month==1)
return \"January\";
else if(month==2)
return \"February\";
else if(month==3)
return \"March\";
else if(month==4)
return \"April\";
else if(month==5)
return \"May\";
else if(month==6)
return \"June\";
else if(month==7)
return \"July\";
else if(month==8)
return \"August\";
else if(month==9)
return \"September\";
else if(month==10)
return \"October\";
else if(month==11)
return \"Novemeber\";
else if(month==12)
return \"December\";
}
void print(int day,int month,int year)
{
//char* months[12]={\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",\"August\",\"September\",\"October\",\"Novemeber\",\"December\"};
printf(\"Valid Date: %s %d, %d\ \",getMonth(month),day,year);
}
//check if its leap year
int isLeapYear(int year)
{
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
return 1;
}
else
return 1;
}
return 0;
}
void isValidDate(int day,int month,int year)
{
//check for valid month
if(month>0 && month<13)
{
//check for feb
if(month==2)
{
//check if it leap year
if(isLeapYear(year)==1)
{
if(day>29)
printf(\"Invalid day value\");
else
print(day,month,year);
}else
{
if(day>28)
printf(\"Invalid day value\");
else
print(day,month,year);
}
}
//end of february month check
//now check for 30 dats
else
if(month==4 || month==6 || month==11 || month==8)
{
if(day>30)
printf(\"Invalid day value.\");
else
print(day,month,year);
}
//now check for 31 days
else
{
if(day>31)
printf(\"Invalid day value.\");
else
print(day,month,year);
}
//end of month validation
}
//display err if invalifd month provided
else
{
printf(\"Invalid Month value \");
}
}
int main()
{
int day,month,year;
printf(\"Please input a date in mm dd yyyy format separated by space or tab\ \");
printf(\"Date: \");
scanf(\"%d%d%d\",&month,&day,&year);
isValidDate(day,month,year);
return 0;
}
Output:
Please input a date in mm dd yyyy format separated by space or tab
Date: 14 16 2000
Invalid month value.
Please input a date in mm dd yyyy format separated by space or tab
Date: 02 29 2015
Invalid date value.
Algorith:
Prompt the user for date
Call isValidDate() by passing params
Check if valid month is provided
If yes,check if month is February.
If yes,check if it\'s leap year. If yes,check for days provided <29
else check for days provided>28
Else check if month is April,June,Sep,Nov.
If yes,check if days provided >31
else
check if days provided <32


