I'm reading a book about C programming, at the end of each chapter it has some training exercise and one of them was to make a program that tells if a date is valid or not, the code below is what I did and I would like to know if it can be improved?#include <stdio.h>int main(){int dd,mm,yy;printf(write a date(day/month/year):);scanf(%d/%d/%d,&dd,&mm,&yy);int bissextile=(yy%4==0)?1:0;if (dd>31 || dd<1 || mm>12 || mm<1)    printf(Invalid date.\n);    else        if((mm==2 && dd<30 && bissextile) || (mm==2 && dd<29 && bissextile==0))            printf(valid date.\n);            else                if((mm==4 || mm==6 || mm==9 || mm==11)&& dd<31)                    printf(valid date.\n);                    else                        if(dd<31 && mm!=2)                            printf(valid date.\n);                            else                                printf(Invalid date.\n);}
Program that tells if a date is valid in C
beginner;c;datetime;validation
Writing a useful function should be the first thing that comes to mind when you have a challenge like this. Functions can be reused, and they can make the logic simpler.In addition, a return value is easy to apply in a function, and you can return whenever the function value is determined.In this case, a function like valid_date which returns a conditional-friendly value (int 1 for OK, 0 for not OK).This function would check for invalid conditions, and return 0 when an invalid condition is found. If all checks pass, though, it returns 1.Your leap-year check is not accurate though. Your code is:(yy%4==0)?1:0;A year is a leap year when:it is a multiple of 4but not if it is also a multiple of 100unless it is also a multiple of 400.A more accurate check is:yy % 400 == 0 || (yy % 4 == 0 && yy % 100 != 0)Putting this advice together, I put up the following method:int valid_date(int dd, int mm, int yy) {    if (mm < 1 || mm > 12) {        return 0;    }    if (dd < 1) {        return 0;    }    int days = 31;    if (mm == 2) {        days = 28;        if (yy % 400 == 0 || (yy % 4 == 0 && yy % 100 != 0)) {            days = 29;        }    } else if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {        days = 30;    }    if (dd > days) {        return 0;    }    return 1;}Note how I calculate the expected limit for days and then had just one check? That makes the code simpler to read too.Finally, that can be used like:int main(){    int dd,mm,yy;    printf(write a date(day/month/year):);    scanf(%d/%d/%d,&dd,&mm,&yy);    printf(%s Date\n, valid_date(dd, mm, yy) ? Valid : Invalid);}