Print Fibonacci Series in reverse order

26 Nov, 2021
Given a number n then print n terms of fibonacci series in reverse order.
Examples: 
Input : n = 5
Output : 3 2 1 1 0

Input : n = 8
Output : 13 8 5 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 the reverse order.
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to print Fibonacci// series in reverse order#include <bits/stdc++.h>using namespace std; void reverseFibonacci(int n){    int a[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        cout << a[i] << " ";    }} // Driver functionint main(){    int n = 5;    reverseFibonacci(n);    return 0;}
// Java Program to print Fibonacci// series in reverse orderimport 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.
# Python 3 Program to print Fibonacci# series in reverse order def reverseFibonacci(n):      a = [0] * n     # assigning first and second elements    a[0] = 0    a[1] = 1     for i in range(2, n):          # storing sum in the        # preceding location        a[i] = a[i - 2] + a[i - 1]           for i in range(n - 1, -1 , -1):          # printing array in        # reverse order        print(a[i],end=" ")         # Driver functionn = 5reverseFibonacci(n)
// C# Program to print Fibonacci// series in reverse orderusing System; 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            Console.Write(a[i] +" ");        }    }         // Driver function    public static void Main()    {        int n = 5;        reverseFibonacci(n);         }} // This code is contributed by vt_m.
<?php// PHP Program to print Fibonacci// series in reverse order function reverseFibonacci($n){     // assigning first and    // second elements    $a[0] = 0;    $a[1] = 1;     for ($i = 2; $i < $n; $i++)    {         // storing sum in the        // preceding location        $a[$i] = $a[$i - 2] +                 $a[$i - 1];    }     for ($i = $n - 1; $i >= 0; $i--)    {         // printing array in        // reverse order        echo($a[$i] . " ");    }} // Driver COde$n = 5;reverseFibonacci($n); // This code is contributed by Ajit.?>
<script> // JavaScript program to print Fibonacci// series in reverse orderfunction reverseFibonacci(n){    let a = [];         // Assigning first and second elements    a[0] = 0;    a[1] = 1;       for(let i = 2; i < n; i++)    {                 // Storing sum in the        // preceding location        a[i] = a[i - 2] + a[i - 1];    }       for(let i = n - 1; i >= 0; i--)    {                 // Printing array in        // reverse order        document.write(a[i] + " ");    }} // Driver Codelet n = 5; reverseFibonacci(n); // This code is contributed by avijitmondal1998 </script>
Output: 
3 2 1 1 0
Time Complexity: O(n)
Auxiliary Space: O(n)
jit_t
avijitmondal1998
samim2000
Fibonacci
Reverse
Dynamic Programming
Dynamic Programming
Fibonacci
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Subset Sum Problem | DP-25
Longest Palindromic Substring | Set 1
Floyd Warshall Algorithm | DP-16
Coin Change | DP-7
Matrix Chain Multiplication | DP-8
Sieve of Eratosthenes
Bellman–Ford Algorithm | DP-23
Find if there is a path between two vertices in an undirected graph
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Find minimum number of coins that make a given value