Generating Fibonacci numbers using two functions. - C++ Function

C++ examples for Function:Function Creation

Description

Generating Fibonacci numbers using two functions.

Demo Code

#include <iostream>
#include <iomanip>

using std::cout;//from w ww  .  j a  v a  2  s.c o m
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;
}

Result


Related Tutorials