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

The following is a function of some class which checks if a positive integer is a Palindrome number…

Computer Science20185 marksFill in
The following is a function of some class which checks if a positive integer is a Palindrome number by returning true or false. (A number is said to be palindrome if the reverse of the number is equal to the original number.) The function does not use modulus (%) operator to extract digit. There are some places in the code marked by ?1?, ?2?, ?3?, ?4?, ?5? which may be replaced by a statement / expression so that the function works properly.
boolean PalindromeNum( int N )
{
    int rev= ?1?;
    int num=N;
    while( num>0)
    {
        int f= num/10;
        int s = ?2?;
        int digit = num-?3?;
        rev= ?4? + digit;
        num /= ?5?;
    }
    if( rev= =N )
        return true;
    else
        return false;
}
(i)[1.0]
What is the statement or expression at ?1?
(ii)[1.0]
What is the statement or expression at ?2?
(iii)[1.0]
What is the statement or expression at ?3?
(iv)[1.0]
What is the statement or expression at ?4?
(v)[1.0]
What is the statement or expression at ?5?

Answer

Answer (i)

AI
$?1? = 0$ - rev is initialised to 0 before the reversed digits are accumulated into it.

Answer (ii)

AI
$?2? = f * 10$ (i.e. $s = f*10$) - since $f = num/10$ is num with its last digit removed (integer division), multiplying it back by 10 gives num with its last digit replaced by 0.

Answer (iii)

AI
$?3? = s$ (i.e. $digit = num - s$) - subtracting $s$ ($=f*10$) from num extracts the last digit of num, without using the modulus operator.

Answer (iv)

AI
$?4? = rev*10$ (i.e. $rev = rev*10 + digit$) - the standard technique to build up the reversed number digit by digit.

Answer (v)

AI
$?5? = 10$ (i.e. $num /= 10$) - removes the last digit of num so the loop processes the next digit in the next iteration. (Verified by running the completed function: PalindromeNum(121) and PalindromeNum(1221) return true, PalindromeNum(123) returns false.)
Implementation of algorithms to solve problems

From ISC 2018 Computer Science Paper 1, question 3.