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.
Explain
Explain why even or odd uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use 9 and explain which part of the rule changes the result.
Even or Odd: follow the data
Given data
A school exercise needs a precise even or odd decision, including the equality case rather than guessing from a typical value.
Rule
An integer is even exactly when dividing it by two leaves remainder zero.
Observed result
Result: even
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
Start with the sample
value starts at 8.
Apply the rule
The predicate value % 2 == 0 selects the label even; the changed sample 9 selects odd.
Check the result
Result: even
Result: even
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.
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.