Fibonacci Sequence Source Code in Java

Hello World ! This time we'd like to share fibonacci sequence source in java. But firstly, we would like to give you short explanation about the sequence.  In mathematics, Fibonacci numbers are sequences that are recursively defined as follows:

{\displaystyle F(n)={\begin{cases}0,&{\mbox{jika }}n=0;\\1,&{\mbox{jika }}n=1;\\F(n-1)+F(n-2)&{\mbox{jika tidak.}}\end{cases}}}

This sequence starts from 0 and 1, then the next number is obtained by adding two consecutive numbers. In short the name of sequence must have a pattern to make a series of number isn't it ? well if you want to know the theory about fibonacci just read this page (it is much easier to understand than wikipedia :p). Then this is source code of program to display fibonacci sequence  :



import java.util.Scanner;

public class Fibonacci { 
    public static void main(String args[]) {
  int a = 1, b = 0, c, n, i = 0;
  System.out.println("Count Fibonanci Numbers!\n "
       + "Input number of series = ");
  Scanner in = new Scanner(System.in);
  n = in.nextInt();
  while(i < n){
   c = a + b;
   a = b;
   b = c;
   i = i + 1;
   System.out.print(" " + c);
   if(i > 20){ System.out.println(" too much"); break;}
  }
  System.out.println(" ... \n");
 }
}


The Result from the listing program above is :



Comments

Popular posts from this blog

Quantization of Image Data Implementation in C#

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Computer Graphics Project "MINION" Source Code with Glut OpenGL