Difference between revisions of "Fibonacci Numbers"

From CompSciWiki
Jump to: navigation, search
Line 41: Line 41:
 
       fibonacci[1] = 1;
 
       fibonacci[1] = 1;
  
       //You start this loop at 2 because you have already calculated the starting two values and you want to start on the third index of the array.
+
       //You start this loop at 2 because you have already calculated the starting two values and you want
 +
      //to start on the third index of the array.
 
       for (int i = 2; i < inputNum; i++)
 
       for (int i = 2; i < inputNum; i++)
 
         fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
 
         fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];

Revision as of 12:20, 6 April 2010

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. Print out the number that is specified.

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

The solution...

Code

Solution Code

Back to the Program-A-Day homepage