Page 424 - Computer science 868 Class 12
P. 424
{
if(n == 1 )
System.out.println();
else if(n % f == 0 )
{ System.out.print(f+" ");
show(n/f,f);}
else
show(n, f+1);
}
}
Ans.
n 27 27 9 3 1
f 2 3 3 3 3
n == 1 false false false false true
n%f == 0 27%2 = 0 false 27%3 = 0 true 9%3 = 0 true 3%3 = 0 true xxx
print xxx 3 3 3 newline
method call show (27,3) show (9,3) show (3,3) print (1,3)
Output
3 3 3
This method is printing the Prime factors of a number
5. The following function Mystery( ) is a part of some class. What will the function Mystery( ) return, when the value of
num = 43629, x = 3 and y = 4? Show the dry run/working. [ISC 2019]
int Mystery (int num, int x, int y)
{
if(num<10)
return num;
else
{
int z = num % 10;
if(z%2 == 0)
return z*x + Mystery (num/10, x, y);
else
return z*y + Mystery(num/10, x, y);
}
}
Ans.
num 43629 4362 436 43 4 4
num<10 false false false false true
z = num%10 9 2 3 6 xxx 4 + 12 = 16
z%2 = 0 9%2 = 0 false 2%2 = 0 true 6%2 = 0 true 3%2 = 0 false xxx 16 + 18 = 34
method call 9*4 2*3+ Mystery(436, 3, 4) 6*3 + 3*4 + Mystery(4,3,4) xxx 34 + 6 = 40
+Mystery (4362, 3, 4) Mystery(43, 3, 4) 40 + 36 = 76
Value returned = 76
6. The following function magicfun() is a part of some class. What will the function magicfun() return, when the value of n = 7 and
n = 10?
Show the dry run/working. [ISC 2017]
int magicfun (int n)
{
422422 Touchpad Computer Science-XII

