C Program to Check a Leap Year

C Program to Check a Leap Year

A school exercise needs a precise leap year decision, including the equality case rather than guessing from a typical value.

A Gregorian year is leap when divisible by 400, or when divisible by 4 but not by 100.

Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why leap year uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use 1900 and explain which part of the rule changes the result.

Leap Year: follow the data

  1. Given data

    A school exercise needs a precise leap year decision, including the equality case rather than guessing from a typical value.

  2. Rule

    A Gregorian year is leap when divisible by 400, or when divisible by 4 but not by 100.

  3. Observed result

    Result: leap

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

A century boundary overrides the usual divisible-by-four rule unless the century is also divisible by 400. Group the alternatives explicitly.

A condition selects a label after the input has been represented as the intended type. The result of a comparison is not a replacement for validating the range of external input.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int value = 2000;
  printf("Result: %s\n", value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) ? "leap" : "not leap");
  return 0;
}

Expected output

Result: leap
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int value = 2000;
  printf("Result: %s\n", value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) ? "leap" : "not leap");
  return 0;
}

value starts at 2000.

The predicate value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) selects the label leap; the changed sample 1900 selects not leap.

Follow the worked calculation

  1. Start with the sample

    value starts at 2000.

  2. Apply the rule

    The predicate value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) selects the label leap; the changed sample 1900 selects not leap.

  3. Check the result

    Result: leap

    Result: leap
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int value = 2000;
  printf("Result: %s\n", value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) ? "leap" : "not leap");
  return 0;
}

Use 1900 and explain which part of the rule changes the result.

Worked answer

The complete answer uses 1900; following the condition produces not leap. Check the condition rather than infer the answer from the input’s appearance.

C17 · complete program

#include <stdio.h>
int main(void) {
  int value = 1900;
  printf("Result: %s\n", value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) ? "leap" : "not leap");
  return 0;
}

Expected output

Result: not leap
Run in C compiler →

Recognize the important boundaries

This is the Gregorian-calendar rule for a positive year; historical local calendar adoption is not modeled.

Use == for equality and = for assignment. Adding an assignment inside a condition can silently replace the value you intended to test.

Check your understanding

Choose the answer, then compare the reasoning for every option.

What must a boundary test verify for this rule?
Choose one answer. You can try again.

Answer and explanation

The exact equality or remainder condition, not only a typical input.

Correct. Boundary values expose comparison mistakes that normal cases can miss.

Use the idea independently

A Gregorian year is leap when divisible by 400, or when divisible by 4 but not by 100.

Continue with combining and interpreting conditions.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Check Vowel or Consonant →