Print Fibonacci Series in reverse order using Recursion

01 Dec, 2021
Given an integer N, the task is to print the first N terms of the Fibonacci series in reverse order using Recursion.
Examples:
Input: N = 5Output: 3 2 1 1 0Explanation: First five terms are – 0 1 1 2 3. 
Input: N = 10Output: 34 21 13 8 5 3 2 1 1 0
Approach: The idea is to use recursion in a way that keeps calling the same function again till N is greater than 0 and keeps on adding the terms and after that starts printing the terms. 
Follow the steps below to solve the problem:
Define a function fibo(int N, int a, int b) whereN is the number of terms anda and b are the initial terms with values 0 and 1.
N is the number of terms and
a and b are the initial terms with values 0 and 1.
If N is greater than 0, then call the function again with values N-1, b, a+b.
After the function call, print a as the answer.
Below is the implementation of the above approach.
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to print the fibonacci// series in reverse order.void fibo(int n, int a, int b){    if (n > 0) {         // Function call        fibo(n - 1, b, a + b);         // Print the result        cout << a << " ";    }} // Driver Codeint main(){    int N = 10;    fibo(N, 0, 1);    return 0;}
// Java program for the above approachimport java.util.*;public class GFG{// Function to print the fibonacci// series in reverse order.static void fibo(int n, int a, int b){    if (n > 0) {         // Function call        fibo(n - 1, b, a + b);         // Print the result        System.out.print(a + " ");    }} // Driver Codepublic static void main(String args[]){    int N = 10;    fibo(N, 0, 1);}}// This code is contributed by Samim Hossain Mondal.
# Python program for the above approach # Function to print the fibonacci# series in reverse order.def fibo(n, a, b):     if (n > 0):         # Function call        fibo(n - 1, b, a + b)         # Print the result        print(a, end=" ")  # Driver Codeif __name__ == "__main__":     N = 10    fibo(N, 0, 1)     # This code is contributed by Samim Hossain Mondal.
// C# program for the above approachusing System;class GFG{// Function to print the fibonacci// series in reverse order.static void fibo(int n, int a, int b){    if (n > 0) {         // Function call        fibo(n - 1, b, a + b);         // Print the result        Console.Write(a + " ");    }} // Driver Codepublic static void Main(){    int N = 10;    fibo(N, 0, 1);}}// This code is contributed by Samim Hossain Mondal.
<script>// Javascript program for the above approach // Function to print the fibonacci// series in reverse order.function fibo(n, a, b){    if (n > 0) {         // Function call        fibo(n - 1, b, a + b);         // Print the result        document.write(a + " ");    }} // Driver Codelet N = 10;fibo(N, 0, 1); // This code is contributed by Samim Hossain Mondal.</script>
  
34 21 13 8 5 3 2 1 1 0 
 
Time Complexity: O(N)Auxiliary Space: O(N)
 
samim2000
Fibonacci
Reverse
Mathematical
Pattern Searching
Recursion
Mathematical
Recursion
Fibonacci
Pattern Searching
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.