Design a program that will accept as input three numbers representing a date in the form day, month, year between 1900 and 2000 and output either OK, if the date is acceptable (represents an actual date), or "NO", if it does not.
C++.
Example usage:
C:\code>checkdate 31 4 1982
NO
C:\code>checkdate 31 3 1982
OK
C:\code>checkdate 29 2 1944
OK
#include <stdio.h>#include <stdlib.h> using namespace std; int main(int argc, char **argv){ if (argc != 4) printf("Usage: checkdate [day] [month] [year]"); else { // days per month int iDaysPerMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // check if it's a leap year if (!(atoi(argv[3]) % 4)) // set max days in february to 29 iDaysPerMonth[1] = 29; // check the date if (atoi(argv[3]) >= 1900 && atoi(argv[3]) <= 2000 && // year atoi(argv[2]) >= 1 && atoi(argv[2]) <= 12 && // month atoi(argv[1]) >= 1 && atoi(argv[1]) <= iDaysPerMonth[atoi(argv[2])-1]) // day printf("OK"); else printf("NO"); }}The PHP solution is very simple because the checkdate function can handle all the checking.
<? // generate a random year between the specified amounts $year = mt_rand(1900, 2000); // generate a day and month // allows invalid values so it can be checked $month = mt_rand(0, 14); $day = mt_rand(0, 35); // show the user the randomly generated date echo $day . '/' . $month . '/' . $year . ' = '; // check if it's valid if (checkdate($month, $day, $year)) // show the user that it is echo 'OK'; else // it's not, tell the user echo 'NO';?>Which produces
21/5/1994 = OK