Draw and explain a flowchart to determine whether an integer is even or odd.
5 marks
Answer in brief
An integer is even if it is divisible by 2 without a remainder. Otherwise it is odd.
Algorithm
Read n: Accept an integer.
Test n % 2 == 0: The % operator gives the remainder of integer division.
Print Even: Use this result when the remainder is zero.
Print Odd: Use this result when the remainder is not zero.
End: Only one of the two results is printed.
Explanation
The condition directly checks divisibility by 2. If the remainder is zero, n can be written as 2 multiplied by an integer. In a flowchart, place this condition inside a decision diamond. Label the true branch Yes and the false branch No. Each branch displays its result and reaches End.
Examples
For n = 8, 8 % 2 is 0, so the output is Even. For n = 7, the remainder is 1, so the output is Odd. Zero is even because 0 = 2 × 0. For n = −3, C gives a remainder of −1; it is non-zero, so the number is still correctly reported as Odd.
Important point
Do not test oddness only by n % 2 == 1 when negative inputs are allowed. A negative odd number can give remainder −1. Testing for a zero remainder and using the other branch for Odd handles both signs. The program also checks that reading the input succeeds before using n. Input values must fit int.
Time and space complexity
One remainder and one comparison are enough. There is no loop, so time is O(1). Only a fixed number of variables is required, giving O(1) extra space.