Fibonacci series in Java

Well, its one thing to claim you know the OOPS Java concepts and entirely different when you start to code with it. I started off with a mess of code and finally thanks to certain code gurus, I could arrange it into OOPS concept and get it working. Most of the Java recursive fibonacci series out there, don’t print the series, but print the last and final number. So here you go. Enjoy. Its kinda basic. But there’s where I start.

Env: Eclipse IDE
Language: Java


package Fibonacci;

public class fibonacciCalculations {

int [] fib;
int size = 0;
public fibonacciCalculations(int k) {
fib = new int [k+1];
size = k+1;
}

void printSeries()
{
for (int i =0;i
System.out.println(fib[i] + "  ");
}
}
public int calculateFibonacci(int k)
{
if(k==0 || k ==1){
fib[k] = k;
}
else  {
fib[k]=calculateFibonacci(k-1)+calculateFibonacci(k-2);

}
return fib[k];

}

}

Save the above within package Fibonacci and name the file fibonacciCalculations


package Fibonacci;

public class callFibonacci{
public static void main(String args[]){
int n = 1;
fibonacciCalculations newFibonacci=new fibonacciCalculations(n);
newFibonacci.calculateFibonacci(n);
newFibonacci.printSeries();

}
}

Save the above within package Fibonacci and name the file callFibonacci

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s