1. Home
  2. Computing & Technology
  3. Java

Counting Rabbits

By Paul Leahy, About.com

Level: Beginner

Focus: Loops

Counting Rabbits Question

In a Fibonacci sequence each number is the sum of the previous two numbers (i.e., it starts with 0,1,1,2,3,5,8 and continues on). The sequence is the result of Fibonacci's attempt in 1202 to find out how many rabbits you get after a certain number of generations. Each number represents how many pairs of rabbits there would be when they are breeding under ideal circumstances. The task is to write a program that calculates and displays the first 22 numbers of a Fibonacci sequence.

Your code will display Fibonacci's hypothesis for 20 generations of rabbits (the first two numbers represent the start of the population and the maturing of the first pair of rabbits).

To get the most out of this question try and figure out the answer before coming back to read the solution on the next page.

Explore Java
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Programming Exercises
  5. Beginner Level
  6. Counting Rabbits>

©2009 About.com, a part of The New York Times Company.

All rights reserved.