Fibonacci series

The Fibonacci series is a very interesting sequence of numbers. It is used by designers and engineers and occurs in nature. You can read about it at http://dcdesign.wikidot.com/wiki:golden-section

This applet uses an array to print out 25 numbers in the Fibonacci sequence. The command

output = Integer.toString(terms[2]);

Converts the integer in terms[2] into a string for printing. It is not really necessary, but it is a useful command to know and it does avoid having to attach a string in front of the integer in the drawString command.

import java.applet.Applet;
import java.awt.*;

public class Fibonacci extends Applet
{

int count, position;
String output;
int[] terms = new int [3];
public void paint (Graphics g)

{

terms[0]=0;
terms[1]=0;
terms[2]=1;
position=10;
for (count=0; count<25; count++)

{

output = Integer.toString(terms[2]);
g.drawString(output,50,position);
position=position+20;
terms[0]=terms[1];
terms[1]=terms[2];
terms[2]=terms[0]+terms[1];

}

}

}

The output is :

Fibonacci.JPG
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License