C Program to Check Even or Odd

C Program to Check Even or Odd

A school exercise needs a precise even or odd decision, including the equality case rather than guessing from a typical value.

An integer is even exactly when dividing it by two leaves remainder zero.

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 even or odd uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

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

Even or Odd: follow the data

  1. Given data

    A school exercise needs a precise even or odd decision, including the equality case rather than guessing from a typical value.

  2. Rule

    An integer is even exactly when dividing it by two leaves remainder zero.

  3. Observed result

    Result: even

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

Understand the operation

Use the remainder operator, not a floating-point division or a test of the last character in the source. Negative even values also have remainder zero.

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 = 8;
  printf("Result: %s\n", value % 2 == 0 ? "even" : "odd");
  return 0;
}

Expected output

Result: even
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int value = 8;
  printf("Result: %s\n", value % 2 == 0 ? "even" : "odd");
  return 0;
}

value starts at 8.

The predicate value % 2 == 0 selects the label even; the changed sample 9 selects odd.

Follow the worked calculation

  1. Start with the sample

    value starts at 8.

  2. Apply the rule

    The predicate value % 2 == 0 selects the label even; the changed sample 9 selects odd.

  3. Check the result

    Result: even

    Result: even
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 = 8;
  printf("Result: %s\n", value % 2 == 0 ? "even" : "odd");
  return 0;
}

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

Worked answer

The complete answer uses 9; following the condition produces odd. Check the condition rather than infer the answer from the input’s appearance.

C17 · complete program

#include <stdio.h>
int main(void) {
  int value = 9;
  printf("Result: %s\n", value % 2 == 0 ? "even" : "odd");
  return 0;
}

Expected output

Result: odd
Run in C compiler →

Recognize the important boundaries

For negative odd integers, do not require remainder +1: C truncates integer division toward zero, so the remainder can be -1.

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

An integer is even exactly when dividing it by two leaves remainder zero.

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 Positive, Negative or Zero →