in Programming in C
578 views
0 votes
0 votes

What does the following function do?
 

 
int fun(unsigned int n)
{
    if (n == 0 || n == 1)
        return n;

    if (n%3 != 0)
        return 0;

    return fun(n/3);
}

 

  1. Returns 1 when n is multiple of 3, otherwise returns 0
  2. Returns 1 when n is the power of 3, otherwise returns 0
  3. Returns 0 when n is multiple of 3, otherwise returns 1
  4. Returns 0 when n is the power of 3, otherwise returns 1
in Programming in C
578 views

4 Comments

edited by
Ans should be option 2. When n is power of 3 for eg 81, you are repeatedly dividing n/3 in each function call and getting the quotient , which will also be a power of 3 . You go on dividing until you reach 3/3 = 1. So 1 will only be returned when n is a power of 3
1
1
Yes, That is right but I am confused between option 1 and option 2, as for example, you had taken 81 which is also the multiple of 3 then how could option 1 is not the answer?
0
0
Multiple of 3 means the statement 1 must hold true for n= all multiples of 3. Does it hold true for 6,12,15 etc and so on?
0
0
Okay, Now I get it. Thank you.
0
0

Please log in or register to answer this question.

Related questions