Calculate Fibonacci numbers. - C++ Data Structure

C++ examples for Data Structure:Algorithm

Description

Calculate Fibonacci numbers.

Demo Code

#include <iostream>
#include <iomanip>
#include <array>
using std::setw;//w ww.  j ava 2  s .c  o  m

int main()
{
  const int n {10};
  std::array<unsigned long long, n> fib;
  fib[0] = fib[1] = 1UL;
  for (int i = 2; i < n; ++i)
    fib[i] = fib[i - 1] + fib[i - 2];

  std::cout << "The first " << n << " Fibonacci numbers are:\n";
  const int perline {5};
  int count {};
  for (auto number : fib)
  {
    std::cout << setw(22) << number;
    if (++count % perline == 0)
       std::cout << std::endl;
  }
}

Result


Related Tutorials