# Reverse fibonacci series

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. In the reverse Fibonacci series, we generate the sequence in reverse order.

## Problem Statement

The problem is to generate and print a reverse Fibonacci series up to a given number n. The series should be printed in reverse order.

For example, if n is 20, the reverse Fibonacci series would be:

4181 2584 1597 987 610 377 233 144 89 55 34 21 13 8 5 3 2 1 1 0

## Approach

To solve this problem, we can use a recursive approach. We define a function, `view_series`, which takes three parameters: the first number, the second number, and the count of numbers remaining to be printed.

In the `view_series` function, we recursively call itself with the second number as the first number and the sum of the first and second numbers as the second number. We decrement the count of numbers remaining to be printed by 1 each time. This recursion continues until the count becomes 0.

During the recursive calls, we print the current first number before making the recursive call.

We also define another function, `fibonacci`, which initializes the first and second numbers as 0 and 1, respectively. It calls the `view_series` function with the initial numbers and the given value of n.

In the `main` function, we set the value of n to 20 and call the `fibonacci` function.

## Pseudocode

```  ```function view_series(first, second, n):
if n > 0:
view_series(second, first + second, n - 1)
print first

function fibonacci(n):
first = 0
second = 1
view_series(first, second, n)

main:
n = 20
fibonacci(n)```

```

## Explanation

The `view_series` function is called with the initial values of first and second as 0 and 1, respectively, and the value of n.

Inside the `view_series` function, the base case is when n becomes 0. In that case, the function returns without making any recursive calls.

Otherwise, it makes a recursive call to `view_series` with the second number as the new first number and the sum of the first and second numbers as the new second number. It also decrements the count of numbers remaining to be printed by 1.

Before making the recursive call, the current value of first is printed. This ensures that the series is printed in reverse order.

The `fibonacci` function initializes the first and second numbers as 0 and 1, respectively, and calls the `view_series` function with these initial values and the given value of n.

In the `main` function, we set n to 20 and call the `fibonacci` function to generate and print the reverse Fibonacci series.

## Time Complexity

The time complexity of this algorithm is O(n) because the `view_series` function is called recursively n times, where n is the given number.

## Code Solution

Here given code implementation process.

``````//C Program
//Reverse fibonacci sequence
#include <stdio.h>

void view_series(int first,int second,int n)
{

if(n > 0)
{
view_series(second,first + second,n-1);

printf("  %d",first );
}
}

void fibonacci(int n)
{
//Set the initial value of variable
//This is two initial value
int first = 0;

int second = 1;

view_series(first,second,n);

}
int main()
{

int n = 20;

fibonacci(n);

return 0;
}```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``
``````/*
C++ Program
Reverse fibonacci sequence
*/
#include <iostream>

using namespace std;
class MyNumber {
public:
void view_series(int first, int second, int n) {
if (n > 0) {
this->view_series(second, first + second, n - 1);
cout << "  " << first;
}
}
void fibonacci(int n) {
int first = 0;
int second = 1;
this->view_series(first, second, n);
}
};

int main() {
MyNumber obj ;
int n = 20;
obj.fibonacci(n);
}```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``
``````/*
Java Program
Reverse fibonacci sequence
*/

public class MyNumber {

public void view_series(int first,int second,int n)
{

if(n > 0)
{
view_series(second,first + second,n-1);

System.out.print("  "+first );
}
}

public void fibonacci(int n)
{
//Set the initial value of variable
//This is two initial value
int first = 0;

int second = 1;

view_series(first,second,n);

}
public static void main(String[] args) {

MyNumber obj = new MyNumber();

int n = 20;

obj.fibonacci(n);
}

}```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``
``````/*
C# Program
Reverse fibonacci sequence
*/
using System;
public class MyNumber {

public void view_series(int first,int second,int n)
{

if(n > 0)
{
view_series(second,first + second,n-1);

Console.Write("  "+first );
}
}

public void fibonacci(int n)
{
//Set the initial value of variable
//This is two initial value
int first = 0;

int second = 1;

view_series(first,second,n);

}
public static void Main(String[] args) {

MyNumber obj = new MyNumber();

int n = 20;

obj.fibonacci(n);
}

}```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``
``````# Ruby Program
# Reverse fibonacci sequence

class MyNumber :
def view_series(self, first, second, n) :
if (n > 0) :
self.view_series(second, first + second, n - 1);
print(first,end="  ");

def fibonacci(self, n) :
first = 0;
second = 1;
self.view_series(first, second, n);

def main() :
obj = MyNumber();
n = 20;
obj.fibonacci(n);

if __name__ == "__main__":
main()```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``
``````# Ruby Program
# Reverse fibonacci sequence

class MyNumber
def view_series(first, second, n)
if (n > 0)
self.view_series(second, first + second, n - 1)
print("  ", first)
end
end
def fibonacci(n)
first = 0
second = 1
self.view_series(first, second, n)
end
end
def main()
obj = MyNumber.new()
n = 20
obj.fibonacci(n)
end
main()```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``
``````<?php
/*
Php Program
Reverse fibonacci sequence
*/

class MyNumber {
public  function view_series(\$first, \$second, \$n) {
if (\$n > 0) {
\$this->view_series(\$second, \$first + \$second, \$n - 1);
echo "  ". \$first;
}
}
public  function fibonacci(\$n) {
\$first = 0;
\$second = 1;
\$this->view_series(\$first, \$second, \$n);
}

}

function main() {
\$obj = new MyNumber();
\$n = 20;
\$obj->fibonacci(\$n);
}
main();```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``
``````/*
Node JS Program
Reverse fibonacci sequence
*/

class MyNumber {
view_series(first, second, n) {
if (n > 0) {
this.view_series(second, first + second, n - 1);
process.stdout.write("  " + first);
}
}
fibonacci(n) {
var first = 0;
var second = 1;
this.view_series(first, second, n);
}
}

function main() {
var obj = new MyNumber();
var n = 20;
obj.fibonacci(n);
}
main();```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``
``````/*
Swift 4 Program
Reverse fibonacci sequence
*/
class MyNumber {

func view_series(_ first: Int, _ second: Int, _ n: Int) {
if (n > 0) {
self.view_series(second, first + second, n - 1);
print(first,terminator:"  ");
}
}
func fibonacci(_ n: Int) {
let first: Int = 0;
let second: Int = 1;
self.view_series(first, second, n);
}
}
func main() {
let obj: MyNumber = MyNumber();
let n: Int = 20;
obj.fibonacci(n);
}
main();```
```

#### Output

``  4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0``

## Output Explanation

The output shows the reverse Fibonacci series up to the given number 20. Starting from the largest number in the series, each number is printed in reverse order.

Here's the explanation of the output:

```  ```
4181  2584  1597  987  610  377  233  144  89  55  34  21  13  8  5  3  2  1  1  0
```
```

The largest number in the series is 4181, followed by 2584, and so on. The series continues in reverse order until it reaches 0.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.