‹ Back to the paper
Question 2(iv)
3 marks
Computer ScienceISC 2026 Improvement Computer Science Paper 1Case basedImplementation of algorithms to solve problems
Fill in the blanks marked ?1?, ?2? ... so that the code works.
boolean isNiven(int n) { int sum = 0, temp = ?1?; while (temp > 0) { sum += ?2?; temp /= 10; }…
boolean isNiven(int n)
{
int sum = 0, temp = ?1?;
while (temp > 0)
{
sum += ?2?;
temp /= 10;
}
return (?3?);
}The following function isNiven( ) is a part of some class which is used to check if a given number is a Niven number or not. There are some places in the code marked by ?1? , ?2? , ?3? which may be replaced by a statement / expression so that the function works properly.
A Niven number is a number which is exactly divisible by the sum of its digits.
Example: 120 = 1+2+0 = 3 and 120 is exactly divisible by 3
(1)[1]
What is the expression or statement at ?1?
(2)[1]
What is the expression or statement at ?2?
(3)[1]
What is the expression or statement at ?3?
Answer
Answer
Blanks: $n$; $temp \% 10$; $(sum \ne 0\ \&\&\ n \% sum == 0)$
?1? = $n$ — initialise temp with n, the number whose digit sum is to be found.
?2? = $temp \% 10$ — adds the last (units) digit of temp to sum.
?3? = $(sum \ne 0\ \&\&\ n \% sum == 0)$ — true only when n is exactly divisible by the digit sum (and sum is non-zero, to avoid division by zero).
(Verified by running the completed function: isNiven(120) returns true, isNiven(123) returns false.)