C++ Algorithm Fibonacci numbers

Description

C++ Algorithm Fibonacci numbers

#include <iostream>
#include <iomanip>
#include <array>
using std::setw;/*from w  ww  . j a va  2 s. co  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;
  }
}
#include <iostream>
#include <iomanip>

using std::cout;/* w  w  w .  jav a 2  s.com*/
using std::cin;
using std::endl;

long& smaller(long& m, long& n);
long& larger(long& m, long& n);

int main(){
  int count {14};

  long n1 {1L};  // First in sequence
  long n2 {1L};  // Second in sequence
  std::cout << std::endl << std::setw(15) << n1 << std::setw(15) << n2;

  for (int i {2}; i < count; ++i){
    if (i % 5 == 0)
      std::cout << std::endl;
    smaller(n1, n2) = n1 + n2;
    std::cout << std::setw(15) << larger(n1, n2);
  }
  std::cout << std::endl;
}

long& smaller(long& m, long& n){
  return m < n ? m : n;
}

long& larger(long& m, long& n){
  return m > n ? m : n;
}



PreviousNext

Related