Sunday, April 29, 2012

Fibonacci Sequence in JAVA!example code

   Here' s the fibonacci sequence. Q:What is fibonacci sequence?
A: This is a pattern of numbers wherein the next number is the sum of two previous number starts with zero(0).How to do this? A simple for looping will do. Here's the catch.


Code:
public class Fibonacci {

    public static void main(String[] args) {
int sunodnaNumber = 1, //initialize
      hulingNumber = 0, //initialize
                      sum;

                      System.out.println("Eto ang Fibonacci Sequence");

               
                //you may use other LOOPING rules. click  here.

                //20 numbers in fibonacci
                 for(int i=0; i<20; i++){

System.out.println(sunodnaNumber);

sum = sunodnaNumber + hulingNumber;
hulingNumber = sunodnaNumber;
sunodnaNumber = sum;

}
    }
}


More examples by request, basta kaya gawin ni author:)
More lessons to come.
If you have more something to add ikakagalak ko po.
SHARE and LEARN!

No comments:

Post a Comment