Home > Backend Development > C++ > C program to check if date is valid

C program to check if date is valid

王林
Release: 2023-09-20 10:17:08
forward
1025 people have browsed it

C program to check if date is valid

The given date format is day, month and year (integer). The task is to determine if that date is feasible.

The valid date range should be 1/1/1800 – 31/12/9999, dates outside these dates are invalid.

These dates include not only the year range, but also all constraints related to calendar dates.

The constraints are -

  • The date cannot be less than 1 and greater than 31
  • The month cannot be less than 1 and greater than 12
  • The year cannot be less than 1800 and Greater than 9999
  • When the month is April, June, September, or November, the date cannot exceed 30.
  • When the month is February, we must check if,
      If the year is a leap year, the number of days cannot exceed 29 days
  • Otherwise, the number of days cannot exceed 28 days.
li>

If all constraints are true, then it is a valid date, otherwise it is not.

Example

Input: y = 2002
   d = 29
   m = 11
Output: Date is valid
Input: y = 2001
   d = 29
   m = 2
Output: Date is not valid
Copy after login

Algorithm

START
In function int isleap(int y)
   Step 1-> If (y % 4 == 0) && (y % 100 != 0) && (y % 400 == 0) then,
      Return 1
   Step 2-> Else
      Return 0
In function int datevalid(int d, int m, int y)
   Step 1-> If y < min_yr || y > max_yr then,
      Return 0
   Step 2-> If m < 1 || m > 12 then,
      Return 0
   Step 3-> If d < 1 || d > 31 then,
      Return 0
   Step 4-> If m == 2 then,
      If isleap(y) then,
         If d <= 29 then,
            Return 1
         Else
            Return 0
         End if
      End if
   Step 5-> If m == 4 || m == 6 || m == 9 || m == 11 then,
      If(d <= 30)
         Return 1
      Else
         Return 0
         Return 1
      End Function
In main(int argc, char const *argv[])
   Step 1->Assign and initialize values as y = 2002, d = 29, m = 11
   Step 2-> If datevalid(d, m, y) then,
      Print "Date is valid"
   Step 3-> Else
      Print "date is not valid&rdquo;
   End main
STOP
Copy after login

Example

Real-time demonstration

#include <stdio.h>
#define max_yr 9999
#define min_yr 1800
//to check the year is leap or not
//if the year is a leap year return 1
int isleap(int y) {
   if((y % 4 == 0) && (y % 100 != 0) && (y % 400 == 0))
      return 1;
   else
      return 0;
}
//Function to check the date is valid or not
int datevalid(int d, int m, int y) {
   if(y < min_yr || y > max_yr)
      return 0;
   if(m < 1 || m > 12)
      return 0;
   if(d < 1 || d > 31)
      return 0;
      //Now we will check date according to month
   if( m == 2 ) {
      if(isleap(y)) {
         if(d <= 29)
            return 1;
         else
            return 0;
         }
      }
      //April, June, September and November are with 30 days
      if ( m == 4 || m == 6 || m == 9 || m == 11 )
         if(d <= 30)
            return 1;
         else
            return 0;
            return 1;
   }
int main(int argc, char const *argv[]) {
   int y = 2002;
   int d = 29;
   int m = 11;
      if(datevalid(d, m, y))
         printf("Date is valid</p><p>");
      else
         printf("date is not valid</p><p>");
      return 0;
}
Copy after login

Output

If you run the above code , it will generate the following output -

Date is valid
Copy after login

The above is the detailed content of C program to check if date is valid. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template