🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1. The sequence typically begins as 0, 1, 1, 2, 3, 5, 8, .... This guide will show you how to generate the Fibonacci series in Java using recursion.
Problem Statement
Create a Java program that:
- Generates the Fibonacci series up to a specified number of terms using recursion.
Example 1:
- Input:
10(first 10 Fibonacci numbers) - Output:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Example 2:
- Input:
5(first 5 Fibonacci numbers) - Output:
0, 1, 1, 2, 3
Solution Steps
- Create a Recursive Method: Write a method that returns the Fibonacci number for a given position.
- Loop to Generate the Series: Use a loop to call the recursive method for the required number of terms.
- Output the Series: Print the Fibonacci numbers.
Java Program
import java.util.Scanner;
/**
* Java Program to Generate Fibonacci Series using Recursion
* Author: https://www.javaguides.net/
*/
public class FibonacciRecursion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get the number of terms from the user
System.out.print("Enter the number of Fibonacci terms: ");
int n = scanner.nextInt();
// Step 2: Generate and print the Fibonacci series
System.out.println("First " + n + " Fibonacci numbers:");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + (i < n - 1 ? ", " : "\n"));
}
}
// Step 3: Recursive method to calculate Fibonacci number
public static int fibonacci(int n) {
if (n <= 1) {
return n; // Base case: return n if n is 0 or 1
} else {
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}
}
}
Explanation
Step 1: Get the Number of Terms from the User
- The program prompts the user to enter the number of terms in the Fibonacci series they want to generate.
Step 2: Generate and Print the Fibonacci Series
- A loop runs from 0 to
n-1, calling thefibonaccimethod for each index. The resulting Fibonacci number is printed. A conditional ensures that commas are placed between numbers, but not after the last number.
Step 3: Recursive Method to Calculate Fibonacci Number
- Base Case: If
nis 0 or 1, the method returnsn. This is because the first two Fibonacci numbers are 0 and 1, respectively. - Recursive Case: For
n > 1, the method calls itself withn-1andn-2and returns their sum. This follows the Fibonacci rule where each number is the sum of the two preceding ones.
Output Examples
Example 1:
Enter the number of Fibonacci terms: 10
First 10 Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Example 2:
Enter the number of Fibonacci terms: 5
First 5 Fibonacci numbers:
0, 1, 1, 2, 3
Conclusion
This Java program uses recursion to generate the Fibonacci series. The recursive approach is straightforward and aligns closely with the mathematical definition of the Fibonacci sequence. However, it can be less efficient for large values of n due to the repeated calculations of the same Fibonacci numbers. In practice, this method is ideal for small to moderate values of n, or for educational purposes to illustrate the concept of recursion.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment