I have created a program that prints the Nth term of the Fibonacci sequence. The fib function needs to use tail recursion. If what I have coded isn't tail recursion, I would like to know how to change the fib function so it does.
#include <iostream>
#include <sstream>
int fib(int n, int i = 0, int a = 0, int b = 1)
{
return (i >= n - 1) ? a : fib(n, i + 1, b, a + b);
}
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cerr << "Argument 2 must be the Nth term." << std::endl;
return -1;
}
std::stringstream ss_obj(argv[1]);
unsigned long int number;
ss_obj >> number;
std::cout << "\nFibonacci number: " << fib(number) << std::endl;
return 0;
}