Fibbonacci Series In JAVA Using Recursion


Fibonacci series in Java
Write a Java program to print the Fibonacci series up to a given number or create a simple Java program to calculate Fibonacci number is common Java questions on fresher interviews and homework. Fibonacci series is also a popular topic on various programming exercises in schools and colleges. Fibonacci series is a series of natural numbers where the next number is equivalent to the sum of the previous two numbers like fn = fn-1 + fn-2. The first two numbers of the Fibonacci series are always 1, 1. In this Java program example for the Fibonacci series, we create a function to calculate Fibonacci numbers and then print those numbers on the Java console.

Another twist in these questions is that sometimes the interviewer asks to write a Java program for Fibonacci numbers using recursion, so it's better you prepare for both iterative and recursive versions of the Fibonacci number.

One more coding question which is quite popular is printing prime numbers in Java which I have discussed earlier. In this tutorial, we will see an example of both recursive and iterative algorithms for the Fibonacci series in Java.



For more coding questions you can always look into Cracking the Code Interviews, one of the finest collections of code-based questions from programming interviews. You will find data structure and algorithmic questions asked on companies like Amazon, Google, Microsoft, and Facebook in this book.

And, if you need to refresh your Data Structure and Algorithms skills to solve those problems then I highly recommend checking out Data Structures and Algorithms: Deep Dive Using Java course on Udemy. It's a hands-on course and covers all essential data structures. It's also very affordable and you can get in just $10 on Udemy flash sales which happen every now and then.

Printing Fibonacci numbers in Java: Sample Code Example

Here is a complete code example of the printing Fibonacci Series in Java. Fibonacci series is calculated using both the Iterative and recursive methods and written in Java programming language. We have two functions in this example, fibonacci(int number)  and  fibonacci2(int number). The first one prints the Fibonacci series using recursion and the second one uses for loop or iteration. 

You can test this code on your computer as well. Once you create your Java source file, just compile and run. It will ask you to enter the number till which you want to see the series. Once you enter then a number, it will print the Fibonacci series in the console.

Java Program to print Fibonacci Series


// Java program for the above approach

class CollegeMantra{

// Function to print N Fibonacci Number
static void Fibonacci(int N)
{
int num1 = 0, num2 = 1;

int counter = 0;

// Iterate till counter is N
while (counter < N) {

// Print the number
System.out.print(num1 + " ");

// Swap
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1;
}
}
         // Driver Code
public static void main(String args[])
{
// Given Number N
int N = 10;

// Function Call
Fibonacci(N);
}
}


After asking to write a simple Java program to print the Fibonacci series and later asking for the Fibonacci series using recursion, another important question interviewer asks is how do you improve your Fibonacci function both iterative and recursive?

A technique called memoization can be used to drastically improve the performance of the method which calculates the Fibonacci number. if you look at the method it repetitively creates the same Fibonacci number.


 

Post a Comment (0)
Previous Post Next Post