Fibonacci Numbers

From CompSciWiki
Revision as of 10:01, 8 April 2010 by JustinS (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

This problem involves you writing a program that will take in which Fibonacci number you want to print out and then iteratively calculates the number. Remember that the algorithm for Fibonacci numbers is Fibonacci[n] = Fibonacci[n-1] + Fibonacci[n-2] where Fibonacci[0] = 0 and Fibonacci[1] = 1. Remember you are going to want to declare your array but not specify the size until you take in your input with JOptionPane:
JOptionPane.showInputDialog()
Remember that you can convert Strings that are numbers to ints using:
Integer.parseInt()
Print out the number that is specified to the console with:
System.out.println();
 

...by students

I remember being back in COMP1010 and looking at the Fibonacci number problem and being slightly intimidated. This is a very common problem that instructors like to give to their students and for good reason. It requires you to take a bunch of different skills you have learned so far and apply them all together. I remember trying to think of ways to solve this problem and the best way I found to do it was by breaking it down into smaller tasks and solving those first. By taking small steps towards the final solution and consistently compiling my code to see if it worked, I was able to solve this problem. This is a technique that I have used throughout my four years in Computer Science and it has become only more valuable as the classes got more difficult. If you take every problem slowly and methodically, you will eventually solve the problem.

Solution

The solution...

Code

Solution Code

Back to the Program-A-Day homepage