## Different ways of writing the Fibonacci Series in Java Program

Category : Java | Sub Category : Java Programs | By Prasad Bonam Last updated: 2020-10-02 08:56:54 Viewed : 747

Different ways of writing  the Fibonacci Series in Java Program

Fibonacci Series: is the collection of numbers in a sequence known as the Fibonacci Series where each number after the first two numbers is the sum of the previous two numbers. The series generally goes like 1, 1, 2, 3, 5, 8, 13, 21 and so on.

fib(n) = fib(n - 1) + fib(n - 2)

## 1. Fibonacci Series – Using For Loop

FibonacciForLoop.java

package runnerdev;

import java.util.Scanner;

public class FibonacciForLoop {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of terms: ");

int n = sc.nextInt();

int i = 0, j = 1, nextTerm;

System.out.println("Fibonacci series is:  ");

for (int f = 0; f < n; f++) {

if (f <= 1)

nextTerm = f;

else {

nextTerm = i + j;

i = j;

j = nextTerm;

}

System.out.println(nextTerm);

}

}

}

Out Put:

## FibonacciWithStatic.javaimport java.util.Scanner;public class FibonacciWithStatic {            public static void main(String[] args) {                        Scanner sc = new Scanner(System.in);                        System.out.println("Enter number of terms: ");                        int n = sc.nextInt();                        series(n);            }            static void series(int num) {                        System.out.println("Fibonacci series is:  ");                        int i = 0, j = 1, next;                        for (int c = 0; c < num; c++) {                                    if (c <= 1)                                                next = c;                                    else {                                                next = i + j;                                                i = j;                                                j = next;                                    }                                    System.out.print(next + " ");                        }            }}OutPut:

##### <Integer> Collector<Integer, ?, List<Integer>> java.util.stream.Collectors.toList()
###### Returns a `Collector` that accumulates the input elements into a new `List`. There are no guarantees on the type, mutability, serializability, or thread-safety of the `List` returned; if more control over the returned `List` is required, use toCollection(Supplier).

Type Parameters:

<T> the type of the input elements

Returns:

a `Collector` which collects all the input elements into a `List`, in encounter order

Syntax:

public static <T>

Collector<T, ?, List<T>> toList() {

(left, right) -> { left.addAll(right); return left; },

CH_ID);

}

FibonacciwithJava8.java

package runnerdev;

import java.util.List;

import java.util.Scanner;

import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class FibonacciwithJava8 {

public static List<Integer> getFibonacci(int series) {

return Stream.iterate(new int[] { 0, 1 }, f -> new int[] { f[1], f[0] + f[1] }).limit(series).map(n -> n[0])

.collect(toList());

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of terms: ");

int num = sc.nextInt();

List<Integer> fibonacci = getFibonacci(num);

System.out.println("Fibonacci series is: ");

fibonacci.forEach(x -> System.out.println(x));

}

}

Output: