Today we are going to discuss another programming question of Java which Fibonacci Series Numbers Sequence Program. So in this blog post, we are going to discuss a few things about Fibonacci numbers:

- What Is The Fibonacci Series?
- Fibonacci Series Formula
- Fibonacci Series Spiral
- Fibonacci Series List

So let us start to discuss the above concepts in detail:

Post Type: | Java Programs Examples |

Published On: | www.softwaretestingo.com |

Applicable For: | Freshers & Experience |

Get Updates: | Join Our Telegram Group |

## What Is The Fibonacci Series?

The Fibonacci series is a sequence of numbers that builds upon itself, with each number being the sum of the previous two numbers. The first two terms are ‘0’ and ‘1’. In some older versions of the series, the term ‘0’ might be omitted.

So the Fibonacci series numbers can be like 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . If you observed that every term can be calculated by adding the two terms before it.

### Fibonacci Series Formula

This Fibonacci numbers formula is mainly used to find the missing numbers of Fibonacci sequence numbers. The formula to find the (n+1)^{th} term in the sequence is defined using the recursive formula, such that F_{0} = 0, F_{1} = 1 to give F_{n}. To find the nth term in the Fibonacci sequence you can use the below formula:

F_{n} = F_{n-1} + F_{n-2}, where n > 1

### Fibonacci Series Spiral

The Fibonacci spiral is a pattern that can be seen in the Fibonacci numbers when they are graphed on a grid. The spiral starts with a rectangle whose length and width follow the “Golden Ratio” (approximately 1.618). This is why it’s called the Golden Rectangle.

Here is an image of a Fibonacci spiral. This type of spiral is created by partitioning a rectangle into 2 squares. The Fibonacci spiral is an approximation of the golden spiral.

### Fibonacci Series List

The Fibonacci Series List is a series of numbers in which each term is the sum of the two terms preceding it. The first 20 numbers in a Fibonacci numbers are given below.

F_{0} = 0 | F_{10} = 55 |

F_{1} = 1 | F_{11} = 89 |

F_{2} = 1 | F_{12} = 144 |

F_{3} = 2 | F_{13} = 233 |

F_{4} = 3 | F_{14} = 377 |

F_{5} = 5 | F_{15} = 610 |

F_{6} = 8 | F_{16} = 987 |

F_{7} = 13 | F_{17} = 1597 |

F_{8} = 21 | F_{18} = 2584 |

F_{9} = 34 | F_{19} = 4181 |

## Fibonacci Sequence Number Program

When we are generating Fibonacci Sequence Number we can do that in various ways like using the recursion and without using recursion.

## Fibonacci Numbers without Using Recursion

If you’re looking to generate the Fibonacci Series without using recursion, there are two ways to go about it.

- Using While loop
- Using for loop

### Fibonacci Numbers Using While Loop

The condition in the while loop is what you need to be careful about when generating Fibonacci Series using a while loop. The code below shows how this works in Java.

package com.softwaretestingo.interviewprograms; public class FibonacciSequenceNumbersEx1 { public static void main(String[] args) { int i = 1, n = 10, firstTerm = 0, secondTerm = 1; System.out.println("Fibonacci Series till " + n + " terms:"); while (i <= n) { System.out.print(firstTerm + ", "); int nextTerm = firstTerm + secondTerm; firstTerm = secondTerm; secondTerm = nextTerm; i++; } } }

Output:

Fibonacci Series till 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

### Print Fibonacci Numbers From 1 to 100 Using While Loop

Instead of displaying the Fibonacci series of a certain number, we are only displaying the series up to the given number. So in this example, we would be displaying the Fibonacci numbers up until 100.

package com.softwaretestingo.interviewprograms; public class FibonacciSequenceNumbersEx3 { public static void main(String[] args) { int n = 100, firstTerm = 0, secondTerm = 1; System.out.println("Fibonacci Series Upto " + n + ": "); while (firstTerm <= n) { System.out.print(firstTerm + ", "); int nextTerm = firstTerm + secondTerm; firstTerm = secondTerm; secondTerm = nextTerm; } } }

Output:

Fibonacci Series Upto 100: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

### Display Fibonacci Series Using for Loop

This program will help you to write a java program that generates the first ‘n’ numbers in the Fibonacci Series using for loops. The logic used here is quite simple. First, initialize the first two numbers of the series. Then use a for loop which adds up its two immediate predecessors and prints out the value. This process continues until your program prints out the first ‘n’ numbers in the series.

package com.softwaretestingo.interviewprograms; public class FibonacciSequenceNumbersEx2 { public static void main(String[] args) { int n = 10, firstTerm = 0, secondTerm = 1; System.out.println("Fibonacci Series till " + n + " terms:"); for (int i = 1; i <= n; ++i) { System.out.print(firstTerm + ", "); // compute the next term int nextTerm = firstTerm + secondTerm; firstTerm = secondTerm; secondTerm = nextTerm; } } }

**Fibonacci Series using Recursion **

Recursion is an awesome java programming technique where a function calls itself directly or indirectly. The related function is called a recursive function. With the help of recursion, certain problems can be easily solved. For example, let’s see how to use recursion to print first ‘n’ numbers of the Fibonacci Series in Java.

If you want to know how to write a recursive Java program to generate the first ‘n’ numbers in the Fibonacci Series, this is the article for you! The logic here is quite simple: first, the user gives the input, and then a for loop is used to iterate until the limit.

Each iteration calls the fibonaccinumber(int n) function, which returns the Fibonacci number at position n. The Fibonacci function recursively calls itself, adding up the previous two Fibonacci numbers each time.

package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class FibonacciSequenceNumbersEx4 { public static void main(String[] args) { int n; System.out.println("Enter how may fibonnaci numbers to print"); Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); for (int i = 0; i<=n-1; ++i) { System.out.print(fibonaccinumber(i) + " "); } } public static int fibonaccinumber(int n) { if(n==0) return 0; else if(n==1) return 1; else return fibonaccinumber(n-1) + fibonaccinumber(n-2); } }

**Output:**

Enter how may fibonnaci numbers to print: 20 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

**Conclusion:**

Well that’s a wrap! We’ve learned how to programmatically print the Nth Fibonacci number using either loop statements or recursion. If you have any concern or confusion then let us know in the comment section.