Fibonacci Numbers Without Recursion

Demonstration of obtaining Fibonacci
Numbers using a simple loop as opposed
to using a recursive function.

Download it here

Fibonacci Numbers are numbers defined mathematically as
n = n_1 + n_2
where n_1 and n_2 are the two number preceding n in the sequence.
Although recursion can be used to solve this sequence easily, recursive functions tend
to have a lot of overhead. Often it is more desirable to have less overhead. In such
cases it would be necessary to use a loop as opposed to a recursive function.

Let's look at some code that solves Fibonacci Numbers without using recursion.

#include <iostream>

//standard namespace
using namespace std;

//function prototype
void fibonacci(int nth);

int main(){
     //local variable
     int nth;

     //get N from user
     cout << "Enter max sequence digit: ";
     cin >> nth;

     //list Fibonacci numbers to Nth degree

     //end program
     return 0;

//fibonacci function
//gets nth fibonacci number without using recursion
void fibonacci(int nth){
     //initial numbers
     int n = 0;
     int n_1 = 1;
     int n_2 = 0;

     //run until Nth number is obtained
     for(; nth>0; nth--){
         n = n_1+n_2; //get Nth number
         n_1 = n_2; //update n_1
         n_2 = n; //update n_2
         //show current number in sequence
         cout << n << endl;