I had this the first day of JAVA class when I was in school.Hope this helps!
//Fibonacci Sequence
import java.io.*;
public class FIBONACCI
{
public static void main(String args[])throws IOException
{
int[] f = new int[20]; //creates array length of 20
f[0] = 1; //first item in array (computer starts counting at 0 not 1)
f[1] = 1; //second item in array
int x; //declares x of type int (integer)
System.out.print(f[0] + "\n" + f[1] + "\n"); //outputs the first 2 elements in array f
for(x=2; x<=19; x++) //notice 'x<=19' not 20 has to be 19 if any higher cannot handle array
{
f[x] = f[x-1] + f[x-2];//calculates next element in array
System.out.println(f[x]); //output next elements in array 2-45
}
}
}
|