How to implement the Fibonacci series in JShell in Java 9?



In this article, we will learn to implement the Fibonacci series in JShell in Java 9. First, we will learn how the Fibonacci series works, then learn two approaches(iterative and recursive) to implement this sequence in the JShell environment.

JShell

JShell is a Java shell tool introduced in Java 9 that allows us to execute Java code and print the result immediately. It is a REPL (Read-Evaluate-Print-Loop) tool that runs from the command-line prompt.

What is a Fibonacci Series?

A number is said to be in the Fibonacci series if each subsequent number is the sum of the previous two numbers. The Fibonacci series starts from two numbers. F0 & F1. The initial values of F0 & F1 can be taken as 0, 1.

The following are the two different approaches for implementing the Fibonacci series in JShell in Java 9:

Implement Fibonacci Series in JShell Using an Iterative Approach

In the iterative approach, we will be using the for loop to implement the Fibonacci series.

Here, we have initialized the two default variables as "x" and "y" with their values as 0 and 1, respectively. A third variable is defined as "z" to store the next number in the series, which is initially assigned a value of 0.

Then we defined a variable as count and stored its value as 5 to print a series up to 8. After defining all the variables, we first print the first 2 values as 0 and 1.

Then we will be using the for loop to iterate up to a count of 5. Inside the loop body, we will add the logic to update the value of x = y and y = z. After that, we will add x and y and store the value in z to and print the value of z.

Example

In the below example, we can able to implement the Fibonacci Series in the JShell using iteration.

C:\Users\User\>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> int x=0, y=1, z=0, count=5;
x ==> 0
y ==> 1
z ==> 0
count ==> 5

jshell> {
...>       System.out.println(x+"\n"+y);
...>       for(int i=0; i<count; i++) {
...>          x=y; y=z;
...>          z = x+y;
...>          System.out.println(z);
...>       }
...>    }
0
1
1
2
3
5
8

Implement Fibonacci Series in JShell Using a Recursive Approach

In the recursive approach, we will be calling the Fibonacci method again and again to print the values of the Fibonacci series. We will be using the following recursive formula to implement the Fibonacci series.

Fn = Fn-1 + Fn-2

Here, we have created a method "printFibonacciSeries" that is used to get the count and print the Fibonacci series in a sequence. Inside this method, we have called a function, fibonacci().

In the fibonacci() function, we have used the recursive formula that is defined above to get all the Fibonacci numbers up to 8.

Example

In the below example, we can able to implement the Fibonacci Series in the JShell using recursion.

jshell> void printFibonacciSeries(int count) {
   ...>     for (int i = 0; i < count; i++) {
   ...>         System.out.print(fibonacci(i) + " ");
   ...>     }
   ...>     System.out.println();
   ...> }
|  created method printFibonacciSeries(int)

jshell> int fibonacci(int n) {
   ...>     if (n <= 1) return n;
   ...>     return fibonacci(n-1) + fibonacci(n-2);
   ...> }
|  modified method fibonacci(int)

jshell> printFibonacciSeries(7)
0 1 1 2 3 5 8
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-06T14:44:13+05:30

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements