Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing
BlueHost Web Hosting

Profollica™ is a standout product in the hair regrowth industry.

New Arrival!  Shop NOW for metal eyeglasses at GlassesShop.com!

Bonsai Boy of New York Bonsai Trees and Accessories

DreamCloud The Comfortable Luxury Mattress Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Liquid Web Cloud VPS Hosting

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

13,696 questions

18,622 answers

573 users

How to check if date is valid in C

1 Answer

0 votes
#include<stdio.h>

int is_date_valid(int d, int m, int y) {
     if (y >= 1900 && y <= 9999) {
        if (m >= 1 && m <= 12) {
            if ((d >= 1 && d <= 31) && (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12))
                return 1;
            else if ((d >= 1 && d <= 30) && (m == 4 || m == 6 || m == 9 || m == 11))
                return 1;
            else if ((d >= 1 && d <= 28) && (m == 2))
                    return 1;
            else if (d == 29 && m == 2 && (y % 400 == 0 ||(y % 4 == 0 && y % 100 !=0)))
                return 1;
            else
                return 0;
        }
        else {
            return 0;
        }
    }
    else {
        return 0;
    }
}

int main() {
    int d = 10, m = 4, y = 2019;

    if (is_date_valid(d, m, y))
        printf("valid");
    else
        printf("not valid");
}


/*
run:

valid

*/

 




answered Apr 10, 2019 by avibootz
...