How to print Fibonacci Series in reverse order.

Given a number n then print n terms of Fibonacci series in reverse order in java.

Examples: 

Input :
n = 5
Output :
3 2 1 1 0

Algorithm :

1) Declare an array of size n. 
2) Initialize a[0] and a[1] to 0 and 1 respectively. 
3) Run a loop from 2 to n-1 and store 
sum of a[i-2] and a[i-1] in a[i]
4) Print the array in reverse order. 

// Java Program to print Fibonacci
// series in reverse order
import java.io.*;

class GFG {
	
	static void reverseFibonacci(int n)
	{
		int a[] = new int[n];
	
		// assigning first and second elements
		a[0] = 0;
		a[1] = 1;
	
		for (int i = 2; i < n; i++)
		{
	
			// storing sum in the
			// preceding location
			a[i] = a[i - 2] + a[i - 1];
		}
	
		for (int i = n - 1; i >= 0; i--)
		{
	
			// printing array in
			// reverse order
			System.out.print(a[i] +" ");
		}
	}
	
	// Driver function
	public static void main(String[] args)
	{
		int n = 5;
		reverseFibonacci(n);
	
	}
}

// This code is contributed by vt_m.

Output: 

3 2 1 1 0

 

Submit Your Programming Assignment Details