Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Click Here
Getting Java Help is Easy!

Join 118,478 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 955 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Fibonacci Sequence

Outputs the fibonacci sequence using arrays very easy to change and alter to output more or less fibonacci numbers, can also be output to a txt file.

Submitted By: pink_floyd
Actions:
Rating:
Views: 9,604

Language: Java

Last Modified: August 14, 2006

Snippet


  1. //Programmed By: pink_floyd
  2. //Date 08/08/06
  3. //Fibonacci     Sequence
  4.  
  5. import java.io.*;
  6.  
  7. public class FIBONACCI
  8. {
  9.      public static void main(String args[])throws IOException
  10.      {
  11.           int[]     f = new int[46]; //creates array length of 46
  12.           f[0] = 1; //first item in array (computer starts counting at 0 not 1)
  13.           f[1] = 1; //second item in array
  14.           int x; //declares x of type int (integer)
  15.          
  16.           System.out.print(f[0] + "\n" + f[1] + "\n"); //outputs the first 2 elements in array f
  17.           for(x=2;     x<=45;     x++) //notice 'x<=45' not 46 has to be 45 if any higher cannot handle array
  18.           {
  19.                f[x] = f[x-1] + f[x-2];//calculates next element in array
  20.                System.out.println(f[x]); //output next elements in array 2-45
  21.           }
  22.      }
  23. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month