- Engineering
- Computer Science
- im getting some errors with this the while loop is...
Question: im getting some errors with this the while loop is...
Question details
I'm getting some errors with this:
-the While loop is not working (the program should keep asking
'Enter a year' until user enters a NEGATIVE year)
-I used %4d because user can only enter 4 digit for year
-Some years like 2800 (which IS a leap year)...the program is
saying that it's not a leap year --> wrong answer
----------------------------------------------------------------
#include < stdio.h >
#include < stdlib.h >
#include < stdbool.h >
bool isLeap(int year);
int main(){
int year;
while (1) {
printf("Enter a year:");
scanf("%4d", &year);
if (year < 0){
return 0;
}
else if (isLeap(year)){
printf("Year %4d is a leap year.", year);
}
else if (!isLeap(year)) {
printf("Year %4d is not a leap year.", year);
}
}
return 0;
}
bool isLeap(int year) {
if (((year / 4 == 0) && (year / 100 != 0)) || (year / 400
== 0))
return true;
return false;
}
Solution by an expert tutor
