C++ Fibonacci Number Sequence using Recursion
Posted by Samath
Last Updated: January 05, 2017

The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth. Written as a rule, the expression is F(n) = F(n-1) + F(n-2).

#include <iostream>
using namespace std;


int fib(int num)
{
  cout << "Finding the fib of " << num << endl;
	if(num == 0)
	{
		cout << "We have reached the termination case of 0. Returning 0" << endl;
		return 0;
	}

	if(num == 1)
	{
		cout << "We have reached the termination case of 1. Returning 1" << endl;
		return 1;
	}

	int result = fib(num - 1) + fib(num - 2);
	cout << "Fib of " << num << " is " << result << endl;
	return result;
}

int main()
{
  fib(6);
  return 0;
}