Monday, 16 June 2014

C++ program to check whether a date is valid or not



Here is a C++ program to check whether a date is valid or not


 SOURCE CODE OUTPUT
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
int main()
{
      int i,dinm[12] = {31, 28, 31, 30, 31, 30,
                               31, 31, 30, 31, 30, 31};
      long day,month,year;
     char sday[10],smonth[10],syear[10];
     cout<<"Enter Date (DD/MM/YYYY) ";
     for(i=0;(sday[i]=getche())!='/';i++);
     for(i=0;(smonth[i]=getche())!='/';i++);
    cin.getline(syear,10);
    day=strtol(sday,0,10);
    month=strtol(smonth,0,10);
    year=strtol(syear,0,10);
    if(month>12||month<0)
       cout<<"Invalid Date";
    else if(day!=dinm[month])
       cout<<"Invalid Date";
    else if(((year%4==0)&&(year%100!=0))||(year%400==0))
    {
        if(month==2&&day!=29)
            cout<<"Invalid date";
    }
    else
    cout<<"Valid Date";
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment