Worked Examples · Example 4.5
Q.Let us write an algorithm to check whether a number is odd or even.
Tamil Nadu DgeTextbookSubjective· 2mImportance★★★★★est
25% · 9/36 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Divide the number by 2 and look at the remainder: remainder 0 means even, otherwise odd — a single IF/ELSE decision (the selection construct).
The idea. This example introduces selection: the algorithm's path depends on a condition. The mathematical fact is that every even number is exactly divisible by 2, so the remainder operator (MOD, % in Python) is the right tool — it answers "what is left over after dividing by 2?" in one step.
Algorithm / pseudocode:
Step 1: INPUT num
Step 2: rem = num MOD 2
Step 3: IF rem == 0 THEN
PRINT "Number is Even"
ELSE
PRINT "Number is Odd"
Step 4: STOP
Flowchart (nodes and arrows):
- Start (oval)
- ↓ Input num (parallelogram)
- ↓ Decision (diamond): "num MOD 2 = 0?"
- Yes → Output "Number is Even" (parallelogram) → Stop
- No → Output "Number is Odd" (parallelogram) → Stop
- Stop (oval)
Python implementation:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Number is Even")
else: …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.