PRASHNIKAप्रश्निका
Back to the paper

The following function isNiven( ) is a part of some class which is used to check if a given number…

Computer Science20263 marksFill in
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
boolean isNiven(int n)
{
    int sum = 0, temp = ?1?;
    while (temp > 0)
    {
        sum += ?2?;
        temp /= 10;
    }
    return (?3?);
}
What are the expressions or statements at ?1?, ?2? and ?3?

Answer

Answer

AI
?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.)
Implementation of algorithms to solve problems

From ISC 2026 Improvement Computer Science Paper 1, question 2(iv).