C bootcamp · Lab 23

Leap year

easyIf Else10 minLesson: If Else

Read the question, write C on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read a year and print yes if it is a leap year, otherwise no.

Divisible by 4, except centuries which must also be divisible by 400.

Input. One integer year.

Output. yes or no.

Constraints

  • 1 ≤ year ≤ 9999

Examples

Example 1
Input
2000
Output
yes
Example 2
Input
1900
Output
no
Example 3
Input
2024
Output
yes
Hint
  1. year % 400 == 0 is always a leap year.
  2. Else year % 4 == 0 && year % 100 != 0.
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <stdio.h>

int main(void) {
  int year;
  if (scanf("%d", &year) == 1) {
    int leap = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    printf(leap ? "yes\n" : "no\n");
  }
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.