Q:

Write a C program to check date is valid or not (2 Methods)

0

Write a C program to check date is valid or not.This program will read date from user and validate whether entered date is correct or not with checking of leap year.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The program given below first accepts a date from the keyboard, determines whether it is valid or not using the algorithm given above and then prints an appropriate message.

 
 

Let us use variables dd,mm and yy (all of type int)to represent the day, month and year in a given date.

The given date is valid only if year (yy)is non-zero, month (mm)is in the range 1 to 12 and day of month (dd) is in the range 1 to mdays, the number of days in a given month mm.

Observe that the %d format Specifiers in the scanf format string are separated by ‘/’ characters as “%d/%d/%d”.

 

Such non-format characters in the format string of the scanf statement must be entered by the user at the indicated position. Thus, the user must enter the date in the format dd/mm/yyyy.


Below is the source code of the C program to check date is valid or not which is successfully compiled and run on the windows system.The Output of the program is shown below.


1 . SOURCE CODE : :

#include <stdio.h>

void main()
{
   int d,m,y;
   int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   int legit = 0;

   printf("Enter the date - DD/MM/YYYY ::  ");
   scanf("%i/%i/%i",&d,&m,&y);

   // leap year checking, if ok add 29 days to february
   if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
    daysinmonth[1]=29;

   // days in month checking
   if (m<13)
   {
      if( d <= daysinmonth[m-1] )
        legit=1;
   }

   if (legit==1)
      printf("It is a valid date!\n");
   else
      printf("It's not valid date!");
}

OUTPUT : : 


Enter the date - DD/MM/YYYY ::  29/02/2015
It's not valid date!

2. Write a C program to check date is valid or not.This program will read date from user and validate whether entered date is correct or not with checking of leap year.


LOGIC : :


  • Enter date.
  • Check year validation, if year is not valid print error.
  • If year is valid, check month validation (i.e. month is between 1 to 12), if month is not valid print error.
  • If month is valid, then finally check day validation with leap year condition, here we will day range from 1 to 30, 1 to 31, 1 to 28 and 1 to 29.
  • If day is valid print date is correct otherwise print error.

Below is the source code of the C program to check date is valid or not which is successfully compiled and run on the windows system.The Output of the program is shown below.


2 . SOURCE CODE : :

#include <stdio.h>
 
int main()
{
    int dd,mm,yy;
     
    printf("Enter date - DD/MM/YYYY : ");
    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;    
}

OUTPUT : : 


Enter date - DD/MM/YYYY : 25/02/2016
Date is valid.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C program to design a digital clock... >>
<< Write a C program to calculate EMI or EMI Calculat...