Java Programs 14: Java Program to Print Fibonacci Series of Given Length Using Recursion

Hello Folks,

This is a frequently asked programming interview question to print a fibonacci series of given length.

In previous post, we have seen printing fibonacci series using iterative logic with collection concept. This will be acceptable for freshers but not for experienced, interviewer expects recursion logic.  This post explains in details how recursion takes place in finding fibonacci series. I assume you should have basic idea about recursion.

Basic definition of Recursion:

Recursion is a programming technique in which a method calls itself till an exit point is satisfied to solve a problem. You can refer this link to know basics of recursion with simple example.

Fibonacci series:

Wikipedia defines it very well.

The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:

Often, especially in modern usage, the sequence is extended by one more initial term:

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

To write recursive method we need to decide two things:
1. Logic to find fibonacci number . (Every number after the first two is the sum of the two preceding ones)
2. Exit point/s where recursive method will be stopped recursion. ( When number position is 1st and 2nd as we know first fibonacci number is 1 and 2nd fibonacci number is 1. )
Recursive method for regression:

Understand by below image:

Complete Java program:


If you like my posts, please like, comment, share and subscribe.


Leave a Reply

Your email address will not be published. Required fields are marked *