Program to Show Fibonacci Collection


fibonacci series in java

Fibonacci collection refers back to the collection the place the next quantity is the addition of the earlier two numbers. The primary two numbers of the collection are normally 0 and 1. 

Instance

enter= 9

output= 0,1,1,2,3,5,8,13,21

Right here, the primary quantity is 0 whereas the second is 1; therefore, the subsequent quantity could be the sum of those two numbers that’s 0+1=1. Equally 1+1=2; 1+2=3, 2+3=5; 3+5=8; 5+8=13; 8+13=21

There are alternative ways or strategies to show the Fibonacci collection. 

Fibonacci Collection in Java with out utilizing recursion

We will keep away from the repeated work we carried out in recursion by the dynamic programming methodology. 

To carry out this, we’d like first to create an array arr[] of measurement N. Then, we have to initialize the array as arr[0]=0; arr[1]=1. After this we iterate the worth of i from 2 to N, and replace the array arr[] as 

arr[i]= arr[i-2] + arr[i-1]. 

Lastly, we print the worth N. 

This syntax is similar as the unique syntax of the Fibonacci collection the one distinction is that we’ve got used an array. 

The implementation is illustrated beneath:

class Fibonaccigreatlearning{
public static void primary(String args[])
{  
 int n1=0,n2=1,n3,i,depend=15;  
 System.out.print(n1+" "+n2);//printing 0 and 1  
  
 for(i=2;i<depend;++i)//loop begins from 2 as a result of 0 and 1 are already printed  
 {  
  n3=n1+n2;  
  System.out.print(" "+n3);  
  n1=n2;  
  n2=n3;  
 }  

}}

Output

On this case, the time complexity and Auxiliary house are the identical: O (N).

Fibonacci Collection utilizing recursion in Java

There are some circumstances satisfying which we will use recursion in Java.

Firstly we would wish the quantity whose Fibonacci collection must be calculated 

Now recursively iterate the worth from N to 1. 

There are the next two circumstances in it:

Base case- right here, if the worth that is named is lower than 1, then the operate returns 1 

Recursive call- the earlier two values are referred to as in case the bottom case just isn’t met. 

The syntax for which is as follows 

recursive_function (N-1) + recursive_function (N-2);

There’s additionally a time period known as recursive operate; it’s referred to as at every recursive name to return the earlier two values for the recursive operate.

Implementation:

class Fibonaccigreatlearning{
 static int n1=0,n2=1,n3=0;  
 static void printFibonacci(int depend){  
    if(depend>0){  
         n3 = n1 + n2;  
         n1 = n2;  
         n2 = n3;  
         System.out.print(" "+n3); 
         printFibonacci(count-1);  
     }  
 }  
 public static void primary(String args[]){  
  int depend=20;  
  System.out.print(n1+" "+n2);//printing 0 and 1  
  printFibonacci(count-2);//n-2 as a result of 2 numbers are already printed 
 }
}

Output– 

Right here the time complexity and Auxiliary house are O(2^N) and O(1), respectively. 

Show Fibonacci Collection Utilizing For Loop

This loop is similar as that of the whereas loop. Firstly, we initialize numbers for the primary two digits, after which we print the collection’s first time period, therefore computing the subsequent time period by the method of Fibonacci. Lastly, perform additional by assigning the worth of the second time period to the primary time period and the subsequent time period to the second time period.

Implementation:

class greatlearning{
  public static void primary(String[] args) {

    int n =17 ,firstTerm = 0, secondTerm = 1;
    System.out.println("Fibonacci Collection until " + n + " phrases:");

    for (int i = 1; i <= n; ++i) {
      System.out.print(firstTerm + ", ");

      // compute the subsequent time period
      int nextTerm = firstTerm + secondTerm;
      firstTerm = secondTerm;
      secondTerm = nextTerm;
    }
  }
}

Output

Show Fibonacci collection utilizing Whereas loop

The whereas loop is the iterative operate the place the primary and second numbers are 0 and 1, respectively. We print these numbers after which ship them to the iterative whereas loop, the place the subsequent quantity is obtained by including the earlier two numbers. Then concurrently, we swap the numbers the place the primary quantity is the second quantity and the second quantity turns into the third.

We will implement the whereas loop as beneath

// Java program for the above strategy

class Greatlearning {

	// Operate to print N Fibonacci Quantity
	static void Fibonacci(int N)
	{
		int num1 = 0, num2 = 1;

		int counter = 0;

		// Iterate until counter is N
		whereas (counter < N) {

			// Print the quantity
			System.out.print(num1 + " ");

			// Swap
			int num3 = num2 + num1;
			num1 = num2;
			num2 = num3;
			counter = counter + 1;
		}
	}

	// Driver Code
	public static void primary(String args[])
	{
		// Given Quantity N
		int N = 18;

		// Operate Name
		Fibonacci(N);
	}
}

Output

Right here the time complexity and auxiliary areas are O(N) and O(1), respectively.

Show the Fibonacci collection as much as a given quantity

Within the earlier circumstances, we outlined the variety of which we printed the Fibonacci collection, however on this case, we print the collection as much as a quantity.

We achieve this by evaluating the primary quantity or time period with n, and if the primary quantity is proved to be lower than n, then the quantity is printed within the collection. If not so, then the collection is assumed to be accomplished. 

We will illustrate it as beneath

class greatlearning {
  public static void primary(String[] args) {

    int n = 88, firstTerm = 0, secondTerm = 1;
        
    System.out.println("Fibonacci Collection Upto " + n + ": ");
    
    whereas (firstTerm <= n) {
      System.out.print(firstTerm + ", ");

      int nextTerm = firstTerm + secondTerm;
      firstTerm = secondTerm;
      secondTerm = nextTerm;

    }
  }
}

Output

Conclusion

With this, we come to the tip of this weblog. To continue learning and upskilling your self, go to the Nice Studying Academy.

Leave a Reply