mardi 5 octobre 2021

Why is my leap year code not working? (Specially int day)

Alright so this is a code from Jupyterlab. And i wanted to know why the int day wont respond to the changes made to the value? The code conering starts from line 37 at //day (January).

#include <stdio.h>
int main() {
   
    
    int year = 2021;
    int month = 1;
    int day = 32; 

    //printf("Enter an integer: ");
    //scanf("%d", &year);

    // true if number is less than 0
    //Year
    if (year > 10000 || year < 1) {
        printf("Error: Invalid year, ");
    }else{
        if(year <= 10000 || year >= 1){
            printf("%d, ", year); 
        }
    }
   
    //Month
    if (month > 12 || month < 1) {
        printf("Error: Invalid month\n");
    }else{
    
        if(month <= 12 || month >= 1){
            printf("%d\n", month); 
        }
    }
    //day (January)
    if ((month == 1) && (day > 1 || day < 31)) {
        printf("%d\n", day);
    }else{
        if ((month == 1) && (day < 1 || day > 31)) {
            printf("Error: Invalid day\n");
        }
    }
   
    //Febuary
    if ((year%400 == 0) && (month == 2) && (day > 1 || day < 29)) {
        printf("%d\n", day); 
    }
    if ((year%400 == 0) && (month == 2) && (day < 1 || day > 29)) {
        printf("Error: Invalid day\n");
    }else{
        if ((month == 2) && (day > 1 || day < 28)) {
            printf("%d\n", day);
        }
        if ((month == 2) && (day < 1 || day > 28)) {
            printf("Error: Invalid day\n");
        }
    }
}

Why is it that from //day (January) the code won't respond to the changes made to the int day? When i enter the date int = 32; it just prints out 32 and not the Error: Invalid day? Why is that and what did i do wrong? [PS. i only have for now January and February...]

Aucun commentaire:

Enregistrer un commentaire