# Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method.

### What is recursion?

Defining anything in terms of itself is called recursion. Recursion is so powerful tool in the programming language that we used to solve many real-life problems easily. When in the body of a function, if any statement calls the same function or another function then that function is recursive in nature. The properties of recursion include the base criteria and progressive approach. Any recursive function should follow these two essential properties.

The Base criteria or conditions define the condition for the function call to stop, and the progressive approach defines that the result should come close to the base criteria after each function call.

• Factorial – The factorial of a number is defined as the product of all the integers less than it and the number itself. It is determined as n! = n * (n-1)!

Q. Write a C++ program to print the factorial of a number using the recursive method.

``````// C++ program to find the factorial of a number using recursive method.
#include <iostream>
using namespace std;

// Function to calculate the factorial of a number through recursion.
int factorial(int n)
{
// Base Criteria
if (n == 0 || n == 1)
{
return 1;
}

// Calculating the factorial using the recursive method.
else
{
return n * factorial(n - 1);
}
}

// Driver Function
int main()
{
// Variable to store the user inputs
int x;
cout << "To exit the program enter a negative number!" << endl;
while (1) // Always True
{
cout << "Enter a number: ";
cin >> x;

// The while loop terminates when the user enters a negative integer.
if (x < 0)
{
cout << "Exited Successfully!" << endl;
break;
}
// Calling the factorial function, passing the value, and storing it in the fact variable.
int fact = factorial(x);

// Ternary operator
fact > 0 ? cout << "The factorial of " << x << " is = " << fact << endl : cout << "Enter a positive number!" << endl;
}

return 0;
}
``````

The possible output of the above program is given below:

``````Enter a negative number to exit the program!
Enter a number: 0
The factorial of 0 is = 1
Enter a number: 4
The factorial of 4 is = 24
Enter a number: 5
The factorial of 5 is = 120
Enter a number: 10
The factorial of 10 is = 3628800
Enter a number: -1
Exited Successfully!
``````

As we know, the factorial of zero and one is 1. Hence, in the base criteria of the factorial function, we return 1 if the value of n is 0 or 1. In the else part, we return the product of factorial (n-1) and n itself.

``````int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
``````

Let us now see the activation record after each function calls and how these recursive functions calls are done:

The above function calls are shown in the figure below and the numbering associated with them tells us which function call is evaluated first:

• Fibonacci Series – In the Fibonacci series, the nth term is equal to the sum of the previous two terms of the series.

Q. Write a C++ program to print the Fibonacci series up to n terms.

``````// C++ program to print the Fibonacci series up to n terms.
#include <iostream>
using namespace std;

// Function to find any term of the Fibonacci series.
int fibonacci_series(int n)
{
// Base Criteria
if (n == 0 || n == 1)
{
return n;
}

// Recursive Method
else
{
return fibonacci_series(n - 1) + fibonacci_series(n - 2);
}
}

// Driver Function
int main()
{
int n, x;
cout << "Enter a number: ";
cin >> n;
cout << "Fibonacci Series up to n terms: ";

// Calling the function n times.
for (int i = 0; i < n; i++)
{
x = fibonacci_series(i);
cout << x << " ";
}
return 0;
}
``````

The sample output of the above program is given below:

``````// Output 1
Enter a number: 5
Fibonacci Series up to n terms: 0 1 1 2 3

// Output 2
Enter a number: 10
Fibonacci Series up to n terms: 0 1 1 2 3 5 8 13 21 34

// Output 3
Enter a number: 15
Fibonacci Series up to n terms: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
``````

In the above method, we have used recursion to find the Fibonacci series. Let us see how the function calls are done in the above method. Assume that n = 5 is passed to the fibonacci_series function. We have used fib for the Fibonacci function.

The above function call is also shown in the figure given below, and the numbering associative with each function calls tell us which function call is evaluated first.