ITPEC FE Subject B April 2026 Question 4
From the answer group below, select the correct combination of answers to be inserted into A through C in the program.
The function isHarshad checks whether the given integer n is a Harshad number. The function returns true if the given number is a Harshad number, or false otherwise. A Harshad number, also known as a Niven number, is a positive integer that is divisible by the sum of its digits. For instance, 156 is a Harshad number because the sum of its digits (1 + 5 + 6 = 12) divides it evenly without a remainder (156 ÷ 12 = 13).
Answer group
Explanation
A: num mod 10, B: integer part of (num ÷ 10), C: n mod sum = 0 — the loop peels digits off the right-hand end, then the original number is tested against their sum.
- •A adds the last digit of
numtosum:num mod 10 - •B then discards that digit:
integer part of (num ÷ 10) - •C — the loop ends only when
numreaches 0, so the test must use the original argument:n mod sum = 0
Trace for 156:
Then 156 mod 12 is 0, so the function returns true.
Answer (f)
Why not others:
- •(a), (b) and (c) — swap A and B: for 156 the loop adds 15 and sets
numto 6, and from then onnum mod 10keepsnumat 6, so the loop never ends - •(e) — tests
num mod sum = 0, butnumis 0 after the loop and 0 mod anything is 0, so every positive input returnstrue - •(d) — tests
sum = n, which holds only for single-digit numbers; 156 has digit sum 12 and would be rejected
Key rule: After a digit-stripping loop the working copy is 0, so any final test must use the original argument; that is why the function copies n into num first.
Math & Numbers · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.