Prashnikaप्रश्निका
Code
About
‹ 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$:
ca
2510
2100
0loop ends
$x*x = 625$, $625 \% 100 = 25$, and $25 == 25$ is true. The function returns true.

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