Ok, i've been working on a leap-year program that will show leap year, month and days. That includes the February 28-29 leap day. I just wanted to know why my code is not working properly. For example i set up january day from 1-31 ad febaury 1-29 in leap year and 1-29 in non-leap year. Also im using jupyterlab as my program and a picture of the error that shows day 32 when it should come up as Error: Invalid day.enter image description here
#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");
}
}
}
Aucun commentaire:
Enregistrer un commentaire