Recursion-1 > bunnyEars

Recursion is a technique that allows us to divide a problem into one or more subproblems that are structurally similar to the original problem.
You must have learned in the college famous recursive problem like

In this article, I will solve one problem named bunnies ears by using recursion. The problem statement is like below

We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication).

bunnyEars(0) → 0  
bunnyEars(1) → 2  
bunnyEars(2) → 4  

SOLUTION

public int bunnyEars(int bunnies)
{
   if (bunnies == 0)
   	return 0;
   else
   	return 2 + bunnyEars(bunnies - 1);

}

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru