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).

[Program]
○ boolean: isHarshad(integer: n)
integer: num ← n
integer: sum ← 0
if (num < 1)
return false
endif
while (num > 0)
sum ← sum + (A)
num ← (B)
endwhile
return (C) // Return true if n is a Harshad number,
// or false otherwise.

Answer group

OptionABC

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 num to sum: num mod 10
  • •B then discards that digit: integer part of (num ÷ 10)
  • •C — the loop ends only when num reaches 0, so the test must use the original argument: n mod sum = 0

Trace for 156:

numlast digit (A)sumnext num (B)
1566615
155111
11120

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 num to 6, and from then on num mod 10 keeps num at 6, so the loop never ends
  • •(e) — tests num mod sum = 0, but num is 0 after the loop and 0 mod anything is 0, so every positive input returns true
  • •(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.