Print nth Term of Fibonacci Series in C, C++, Java | FACE Prep

Print nth Term of Fibonacci Series in C, C++, Java | FACE Prep

Print nth Term of Fibonacci Series in C, C++, Java | FACE Prep

The Fibonacci series is one of the most famous mathematical sequences, often used in algorithmic problems and coding challenges. In this article, we’ll walk you through how to print the nth term of the Fibonacci series using both iterative and recursive methods. Understanding this concept is essential for anyone learning programming, as it helps build problem-solving skills.

What is the Fibonacci Series?

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. Mathematically, it can be represented as:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…For example, to find the 8th term in the series, we simply add the 6th and 7th terms:5 + 8 = 13.So, the 8th term in the Fibonacci sequence is 13.

Method 1: Printing the nth Fibonacci Term Using Iteration

The iterative method involves using a loop to calculate the Fibonacci series up to the nth term. This method is efficient in terms of time complexity and is easy to understand.

Code for Iterative Method:

python
# Python program to find the nth Fibonacci number (Iterative method)def fibonacci(n): # Initialize the first two terms of the Fibonacci sequence a, b = 0, 1 for _ in range(n): a, b = b, a + b return a# Example: Find the 8th Fibonacci number n = 8 print(f”The {n}th term of the Fibonacci series is {fibonacci(n)})
Time Complexity: O(n) Space Complexity: O(1) 

Method 2: Printing the nth Fibonacci Term Using Recursion

The recursive method calls the same function to calculate the Fibonacci numbers, breaking the problem into smaller subproblems. While elegant, recursion can be less efficient for large inputs due to its high time complexity.

Code for Recursive Method:

python
# Python program to find the nth Fibonacci number (Recursive method)def fibonacci(n): if n <= 1: return n return fibonacci(n – 1) + fibonacci(n – 2)# Example: Find the 8th Fibonacci number n = 8 print(f”The {n}th term of the Fibonacci series is {fibonacci(n)})
Time Complexity: O(2^n) (inefficient for large values of n) Space Complexity: O(n)

Comparison: Iterative vs Recursive Approach

Both methods will give you the correct Fibonacci number, but each has its advantages and trade-offs.
MethodTime ComplexitySpace ComplexityBest for
IterativeO(n)O(1)Large inputs, efficient
RecursiveO(2^n)O(n)Smaller inputs, educational


Conclusion

Printing the nth term of the Fibonacci series is a classic exercise for programmers. By mastering both the iterative and recursive methods, you’ll not only gain a deeper understanding of algorithms but also enhance your ability to solve coding challenges efficiently.For larger problems, the iterative method is typically preferred due to its efficiency, but understanding recursion will always be valuable for building strong problem-solving skills.
c