Fibonacci Series in Java [Case-Study Based]

Fibonacci Series in Java [Case-Study Based]

4 mins read687 Views Comment
Atul
Atul Harsha
Senior Manager Content
Updated on Jul 26, 2023 18:09 IST

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In Java, the Fibonacci series can be implemented using loops, recursion, or a combination of both.

2023_01_Feature-Image-Templates-76.jpg

Problem: Write a Java program that generates the first n numbers in the Fibonacci series, starting with 0 and 1. The Fibonacci series is a sequence of numbers in which each number is the sum of the previous two numbers in the series.

For example, the first 10 numbers in the Fibonacci series are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Input: An integer n (1 <= n <= 50), which represents the number of numbers to generate in the Fibonacci series.
Output: fib(10) => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Fibonacci Series Program in C++
Fibonacci Series in Python
Fibonacci Program in C

Solution 1: Fibonacci Series in Java using for loop

  • If n is 0, return 0.
  • Initialize the variables previous and current to 0 and 1, respectively. Initialize the variable next to 0.
  • Start a loop that iterates n – 1 times.
  • In each iteration of the loop:
    • Calculate the next value in the sequence as the sum of previous and current.
    • Update previous to the value of current.
    • Update current to the value of next.
  • Return the value of current.

CODE:


 
class Fibonacci {
public int fib(int n) {
// base case: if n is 0, return 0
if (n == 0) {
return 0;
}
// initialize the previous, current, and next values
int previous = 0;
int current = 1;
int next = 0;
// loop through the remaining values of the sequence
for (int i = 1; i < n; i++) {
// calculate the next value in the sequence
next = previous + current;
// update the previous and current values
previous = current;
current = next;
}
// return the current value
return current;
}
}
Copy code
Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
β‚Ή15 K
3 months
β‚Ή2.05 K
3 months
– / –
170 hours
β‚Ή5.4 K
1 month
– / –
6 months
– / –
2 months
β‚Ή17 K
3 months
β‚Ή5.5 K
1 month

Solution 2: Fibonacci Series in Java using while loop


 
public class Fibonacci {
public static void main(String[] args) {
// initialize the first two values of the series
int previous = 0;
int current = 1;
// print the first two values
System.out.print(previous + " " + current + " ");
// initialize the next value in the series
int next = 0;
// loop through the remaining values of the series
while (next < 100) {
// calculate the next value in the series
next = previous + current;
// print the next value
System.out.print(next + " ");
// update the previous and current values
previous = current;
current = next;
}
}
}
Copy code

The code initializes the first two values of the Fibonacci series (0 and 1) and prints them. It then initializes a variable next to 0 and enters a while loop. While loop executes till next is less than 100.

  • Inside the while loop, the code:
    • Calculates the next value in the series by adding the previous and current values and stores it in the next variable.
    • Prints the value of next.
    • Updates the previous and current values to the current values of current and next, respectively.
  • The loop continues this process until the value of next becomes greater than or equal to 100, at which point the loop will exit.

Solution 3: Fibonacci Series in Java without using loop


 
class Fibonacci {
public int fib(int n) {
// base case: if n is 0, return 0
if (n == 0) {
return 0;
}
// base case: if n is 1, return 1
else if (n == 1) {
return 1;
}
// recursive case: if n is greater than 1,
// return the sum of the previous two numbers in the sequence
else {
return fib(n - 1) + fib(n - 2);
}
}
}
Copy code

This version of the code uses recursion to calculate the nth Fibonacci number. The recursion stops when n is 0 or 1, and the Fibonacci number is returned directly. For all other values of n, the function calls itself with n – 1 and n – 2 as arguments, and returns the sum of the two returned values.

Maximizing Field Area with the Fibonacci Series: A Case Study for Efficient Fencing

Problem: A farmer wants to fence off a rectangular field using the least amount of fencing material possible. The field is bordered by a river on one side, so the farmer only needs to fence the other three sides. The farmer has a roll of fencing material that can be used to build the fence, and the roll has a fixed length.

288 Farm Fence Post Illustrations & Clip Art - iStock

The farmer wants to build the fence in such a way that the area of the field is maximized, given the fixed length of the fencing material. Use the Fibonacci series to determine the optimal dimensions of the field, because the ratio of the length to the width of the field will approach the golden ratio (approximately 1.618) as the length and width get larger.

Write a Java program that calculates the maximum possible area of the field, given the fixed length of the fencing material and the desired number of terms in the Fibonacci series to use.

Input:

  • An integer n (1 <= n <= 50), which represents the number of terms in the Fibonacci series to use.
  • An integer L (1000 <= L <= 10000), which represents the length of the fencing material in meters.

Output:

  • An integer, representing the maximum possible area of the field in square meters.

Solution:


 
public class FieldFencing {
public static void main(String[] args) {
int n = 10; // number of terms in the Fibonacci series
int L = 5000; // length of fencing material in meters
// initialize the previous and current values of the Fibonacci series
int prev = 1;
int current = 1;
int next = 0;
// loop through the remaining terms of the Fibonacci series
for (int i = 2; i < n; i++) {
// calculate the next term in the series
next = prev + current;
// update the previous and current values
prev = current;
current = next;
}
// calculate the maximum possible area of the field
int width = L / current;
int length = L - width;
int area = width * length;
System.out.println("Maximum possible area of the field: " + area + " square meters");
}
}
Copy code

This program generates the first n terms in the Fibonacci series. It calculates the maximum possible area of the field by dividing the length of the fencing material by the nth term in the series. The width of the field is equal to this value, and the length of the field is equal to the remaining fencing material. The area of the field is then calculated as the product of the width and length.

FAQs

What is Fibonacci Series?

Fibonacci numbers are the sequence of numbers defined by the linear equation, F(n) = F(n-1) + F(n-2), for n = 3, 4, ... and F(0) = 0, F(1) = F(2) = 1.

What are the first 10 Fibonacci Numbers in the Fibonacci Series?

The first 10 Fibonacci numbers are, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

What is the use of Fibonacci Series?

Fibonacci Series is used in Finance (Stock Market Analysis), Computer Science (Efficient Search), Music ( Tuning Musical Instruments), Art (Creating Pleasing Visual Design).

About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio