Write a C Program to check whether a date is valid or not

Admin

 Write a C Program to check whether a date is valid or not



#include<stdio.h>
int main(void)
{
    int d,m,y;
    int flag=1,isleap=0;

    printf("Enter date(dd/mm/yyyy): ");
    scanf("%d/%d/%d",&d,&m,&y);
    if(y%100!=0 && y%4==0  ||  y%400==0)
        isleap=1;

    if(y<1850 || y>2050 || m<1 || m>12 || d<1 || d>31)
        flag=0;
    else if(m==2)
    {
        if(d==30 || d==31 || (d==29 && !isleap) )
            flag=0;
    }   
    else if(m==4 || m==6 || m==9 || m==11)
    {   
        if(d==31)
            flag=0;
    }
    
    if(flag==0)
        printf("Not a valid date\n");
    else
        printf("Valid Date \n");
    return 0;
}   


Output



Enter date(dd/mm/yyyy): 15/07/2019
Valid Date 

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.