Category : Java | Sub Category : Java Programs | By Prasad Bonam Last updated: 2020-10-02 14:26:54 Viewed : 398
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)
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);
}
}
}
FibonacciWhileLoop.java
package
runnerdev;
import
java.util.Scanner;
public class
FibonacciWhileLoop {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter
number of terms: ");
int n =
sc.nextInt();
int first
= 0, second = 1, next;
System.out.println("Fibonacci
series is: ");
int i =
0;
while (i
< n) {
if (i
<= 1)
next = i;
else {
next = first +
second;
first = second;
second = next;
}
System.out.println(next);
i++;
}
}
}
OutPut:
FibonacciWithStatic.java
import
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:
package
runnerdev;
import
java.util.Scanner;
public class
FibonacciRecursion {
int f = 0, i = 0, j = 1, next;
void fib(int n) {
if (n
> 0) {
if (f
<= 1)
next = f;
else {
next = i + j;
i = j;
j = next;
}
System.out.print(next + "
");
f++;
fib(--n);
}
}
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter
number of terms: ");
int num =
sc.nextInt();
FibonacciRecursion f = new
FibonacciRecursion();
System.out.println("Fibonacci
series is: ");
f.fib(num);
}
}
Output:
FibonacciIfCondition.java
import
java.util.Scanner;
public class
FibonacciIfCondition {
public static int fib(int n) {
if (n
<= 1)
return n;
else
return fib(n
- 1) + fib(n - 2);
}
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter
number of terms: ");
int num =
sc.nextInt();
System.out.println("Fibonacci
series is: ");
for (int i =
0; i < num; i++) {
System.out.println(fib(i));
}
}
}
Output:
Returns an infinite sequential ordered Stream
produced by iterative application of a function f
to an initial element seed
,
producing a Stream
consisting of seed
, f(seed)
,
f(f(seed))
, etc.
The first element (position 0
) in the
Stream
will be the provided
seed
. For n > 0
, the element at position n
, will be the result of applying the function f
to the element at position n - 1
.
Type Parameters:
<T>
the type of stream elements
Parameters:
seed
the initial element
f
a function to be applied to to the previous element to produce a new element
Returns:
a new sequential Stream
Syntax:
public static<T>
Stream<T> iterate(final T seed, final
UnaryOperator<T> f) {
Objects.requireNonNull(f);
final
Iterator<T> iterator = new Iterator<T>() {
@SuppressWarnings("unchecked")
T t = (T)
Streams.NONE;
@Override
public boolean
hasNext() {
return true;
}
@Override
public T
next() {
return t = (t ==
Streams.NONE) ? seed : f.apply(t);
}
};
return
StreamSupport.stream(Spliterators.spliteratorUnknownSize(
iterator,
Spliterator.ORDERED |
Spliterator.IMMUTABLE), false);
}
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() {
return new
CollectorImpl<>((Supplier<List<T>>) ArrayList::new,
List::add,
(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: