Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 149,019 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,026 people online right now. 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: 11,712

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.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month