ISRO Scientist/Engineer CSE 2020Programming in C › Not yet assigned to a topic
ISRO Scientist/Engineer CSE 2020 PYQ: Consider the following recursive C function that takes two arguments unsigned int rer(unsigned int n,…
Consider the following recursive C function that takes two arguments
unsigned int rer(unsigned int n, unsigned int r){
if(n>0)return(n%r + rer(n/r,r));
else retturn 0;
} What is the return value of the function $rer$ when it is called as $rer(513,2)$?
- A9
- B8
- C5
- D2Correct answer
Answer
(D) $2$
Approach
The function recursively computes the sum of remainders when dividing n by r repeatedly until n becomes 0. This is equivalent to counting the number of 1s in the binary representation of n when r=2.
Reviewed by ISRO IQ Editorial Team.