Fibonacci Series in Java

The Fibonacci series program in java using recursion and looping statements. In Java, a Fibonacci Series is a set of numbers where the next number is the sum of the previous two. The Fibonacci series begins with the digits 0 and 1. The Fibonacci numbers play an important role in determining the greatest common divisor of two integers in a computational run-time study. The Wythoff array is an infinite matrix of numbers derived from the Fibonacci sequence in arithmetic.

Unknown fact of fibonacci series

It represents structures and sequences that model physical reality, the Fibonacci sequence emerges in nature. Because it intrinsically models a sort of spiral, we see it in the spiral patterns of certain flowers.

Click here to read more

Fibonacci series program in java using for loop

public class main
{
public static void main(String[] args)
{
int limit = 10;
int last = 0;
int upcoming = 1;
System.out.print("Fibonacci series "+limit+" numbers");
for (int i = 1; i <= limit; ++i)
{
System.out.print(last+" ");
int sum = last + upcoming;
last = upcoming;
upcoming = sum;
}

}

}

 

Output:

Fibonacci series

5

numbers

0 1 1 2 3

What is for loop in java programming

A for loop is a repetition control structure that allows you to design a loop that must be repeated a certain number of times quickly. When you know how many times a task will be repeated, a for loop comes in handy.

Fibonacci series program in java using while loop

public class Main
{
public static void main(String[] args)
{
int limit = 10, last = 0, upcoming = 1;
System.out.print("Fibonacci Series of "+limit+" numbers");
int i=1;
while(i <= limit)
{
System.out.print(last+" ");
int sum = last + upcoming;
last = upcoming;
upcoming = sum;
i++;
}
}
}

 

Output:

Fibonacci Series of 10 numbers

0 1 1 2 3 5 8 13 21 34 

What is while loop in java programming

The condition is assessed first in a while loop, and if it returns true, the statements inside the while loop are executed. This process is repeated until the condition returns false. When the condition returns false, the control exits the while loop and moves on to the next sentence in the program.

Fibonacci series program in java using recursion method

public class Main
{
public static int fibonacciRecursion(int n)
{
if(n == 0){
return 0;
}
if(n == 1 || n == 2)
{
return 1;
}
return fibonacciRecursion(n-2) + fibonacciRecursion(n-1);
}
public static void main(String args[])
{
int limit = 10;
System.out.print("Fibonacci Series of "+limit+" numbers: ");
for(int i = 0; i < limit; i++){
System.out.print(fibonacciRecursion(i) +" ");
}
}
}

 

Output:

Fibonacci Series of 10 numbers: 0 1 1 2 3 5 8 13 21 34

What is recursion in java programming

The technique of repeating objects in a self-similar manner is known as recursion. Recursion is a feature of the C programming language, which allows a function to call itself. However, when employing recursion, programmers must ensure that the function has an exit condition, or else it would enter an indefinite loop.