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++
// 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 function int main() { int n = 5; reverseFibonacci(n); return 0; } |
Java
// 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. |
Python3
# 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 function n = 5 reverseFibonacci(n) |
C#
// C# Program to print Fibonacci // series in reverse order using 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 // 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. ?> |
Output:
3 2 1 1 0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C++ Program to print Fibonacci Series using Class template
- 3 Different ways to print Fibonacci series in Java
- C Program to print Fibonacci Series without using loop
- Print words of a string in reverse order
- Print Doubly Linked list in Reverse Order
- Program to print numbers from N to 1 in reverse order
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- n'th multiple of a number in Fibonacci Series
- C/C++ Program for nth multiple of a number in Fibonacci Series
- Factorial of each element in Fibonacci series
- Find n terms of Fibonacci type series with given first two terms
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- K- Fibonacci series
- Python | Find fibonacci series upto n using lambda
- Find the Nth element of the modified Fibonacci series
- Last digit of sum of numbers in the given range in the Fibonacci series
- Check whether Array represents a Fibonacci Series or not
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Reverse Level Order Traversal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t

