Question #273837

WAP to enter a date (dd,mm,yyyy) . Throw exception if the mm value is not in the range

1 to 12. Throw another exception if the dd value is not in the range as per the month entered. Display the message corresponding to the exception thrown. Include catch all block.


Expert's answer

/*C program to validate date (Check date is valid or not).*/
 
#include <stdio.h>
 
int main()
{
    int dd,mm,yy;
     
    printf("Enter date (DD/MM/YYYY format): ");
    scanf("%d/%d/%d",&dd,&mm,&yy);
     
    //check year
    if(yy>=1900 && yy<=9999)
    {
        //check month
        if(mm>=1 && mm<=12)
        {
            //check days
            if((dd>=1 && dd<=31) && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12))
                printf("Date is valid.\n");
            else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11))
                printf("Date is valid.\n");
            else if((dd>=1 && dd<=28) && (mm==2))
                printf("Date is valid.\n");
            else if(dd==29 && mm==2 && (yy%400==0 ||(yy%4==0 && yy%100!=0)))
                printf("Date is valid.\n");
            else
                printf("Day is invalid.\n");
        }
        else
        {
            printf("Month is not valid.\n");
        }
    }
    else
    {
        printf("Year is not valid.\n");
    }
 
    return 0;    
}
LATEST TUTORIALS
APPROVED BY CLIENTS