Skip to content
Related Articles

Related Articles

Algorithms | Recursion | Question 7
  • Difficulty Level : Easy
  • Last Updated : 03 Jul, 2020
GeeksforGeeks - Summer Carnival Banner

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);
}

(A) It returns 1 when n is a multiple of 3, otherwise returns 0
(B) It returns 1 when n is a power of 3, otherwise returns 0
(C) It returns 0 when n is a multiple of 3, otherwise returns 1
(D) It returns 0 when n is a power of 3, otherwise returns 1


Answer: (B)

Explanation: Lets solve with example, n = 27 which power of 3.
First time if condition is false as n is neither equal to 0 nor equal to 1 then 27%3 = 0.
Here, again if condition false because it is equal to 0.
Then fun(27/3) will call.

Second time if condition is false as n is neither equal to 0 nor equal to 1 then 9%3 = 0.
Here again if condition false because it is equal to 0.

Then fun (9/3) will call and third time if condition is false as n is neither equal to 0 nor equal to 1 then 3%3 = 0.
Here again if condition false because it is equal to 0.

Then fun(3/3) will call here n==1 if condition gets true and it return n i.e. 1.

Option (B) is correct.

Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :