Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

January 3, 2010

Fibonacci series -> w/o using recursion

// Print out the first N Fibonacci numbers in the order:
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
public static final void printFib(final int N) {

int f0 = 0;
int f1 = 1;

for (int i = 0; i < N; ++i) {
System.out.println(f0);

final int temp = f1;
f1 += f0;
f0 = temp;
}
}

No comments: