‹ Back to the paper
Question 2(iii)
3 marks
Computer ScienceISC 2023 Computer Science Paper 1Case basedMethods
Answer the following.
boolean num(int x) { int a=1; for (int c=x; c>0; c/=10) a *= 10; return (x*x%a)==x; } With…
boolean num(int x)
{ int a=1;
for (int c=x; c>0; c/=10)
a *= 10;
return (x*x%a)==x;
}With reference to the code given below, answer the questions that follow along with dry run / working.
(a)[2]
What will the function num() return when the value of $x=25$?
(b)[1]
What is the method num() performing?
Answer
Answer (a)
Dry run for $x = 25$:
$x*x = 625$, $625 \% 100 = 25$, and $25 == 25$ is true.
The function returns true.
| c | a |
| 25 | 10 |
| 2 | 100 |
| 0 | loop ends |
Answer (b)
It checks whether x is an Automorphic number, i.e. whether the square of the number ends with the number itself (a is 10 raised to the number of digits of x).