LearnFrenzy
Sign in Take a free test

Decision Making · Predict the Output · Question 10

Leap years with a nested conditional expression Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int years[] = {1900, 2000, 2023, 2024};
    for (int i = 0; i < 4; i++) {
        int y = years[i];
        putchar(y % 400 == 0 ? 'L' : y % 100 == 0 ? 'N' : y % 4 == 0 ? 'L' : 'N');
    }
    putchar('\n');
    return 0;
}
  • A NLNL Correct
  • B LLNL
  • C NLLL
  • D LNNL
Correct Answer: Option A

The tests run from the most specific rule to the most general: divisible by 400 → leap; else by 100 → not leap; else by 4 → leap. 1900 is divisible by 100 but not 400, so it is not a leap year.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links