• Source
    1. // We have a number of bunnies and each bunny has two big floppy ears.
    2. // We want to compute the total number of ears across all the bunnies
    3. // recursively (without loops or multiplication).
    4. //bunnyEars(0) → 0
    5. //bunnyEars(1) → 2
    6. //bunnyEars(2) → 4
    7.  
    8. #include <stdio.h>
    9.  
    10. // To enable debug messages uncomment #define
    11. #define TEST 1
    12.  
    13. int bunnyEars(int bunnies);
    14. void startTesting();
    15.  
    16. int main(void) {
    17. #ifdef TEST
    18. startTesting();
    19. #endif
    20.  
    21. return 0;
    22. }
    23.  
    24. int bunnyEars(int bunnies) {
    25. if (bunnies == 0) {
    26. return 0;
    27. }
    28. if (bunnies == 1) {
    29. return 2;
    30. }
    31.  
    32. return 2 + bunnyEars(bunnies - 1);
    33. }
    34.  
    35. void startTesting()
    36. {
    37. int result = 0;
    38. int i = 0;
    39.  
    40. for (i = 0; i <= 5; i++) {
    41. result = bunnyEars(i);
    42.  
    43. printf("BunnyEars(%d) = %d\n", i, result);
    44. }
    45. }
    46.