(iv) The following function quiz( ) is a part of some class. Assume ā€˜nā€™ is a positive integer, greater than 0. Answer the given questions along with dry run / working.

int quiz( int n)
{
if ( n <= 1 )
return n;
else
return (--n % 2) + quiz(n/10);
}

(a) What will the function quiz( ) return when the value of n=36922?
(b) State in one line what does the function quiz( ) do, apart from recursion?


Solution



(a) quiz(36922)
(36921 % 2) + quiz(3692)
(3691 % 2) + quiz(369)
(368 % 2) + quiz(36)
(35 % 2) + quiz(3)
(2 % 2) + quiz(0)
Output:
0 + 1 + 0 + 1 + 1 = 3
(b) Counting the number of even digit in the number.


Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com