Skip to content

Instantly share code, notes, and snippets.

@ahmet-cetinkaya
Created November 27, 2020 09:46
Show Gist options
  • Save ahmet-cetinkaya/4052626e410b85916886a0162515432d to your computer and use it in GitHub Desktop.
Save ahmet-cetinkaya/4052626e410b85916886a0162515432d to your computer and use it in GitHub Desktop.
C++ Fibonacci Recursive Functions
#include<iostream>
using namespace std;
// @params a is first number.
// @params b is second number.
// @params c is the upper limit of numbers.
int fobi(int a, int b, int c) {
int next = a + b;
if (next > c) return 0;
cout << next << " ";
return fobi(next, a, c);
}
// @params c is number of outputs.
int fibo2(int c) {
if (c == 1 || c == 0) return c;
return (fibo2(c - 1) + fibo2(c - 2));
}
main() {
fobi(0, 1, 100);
for (int i = 1; i < 12; i++) cout << " " << fibo2(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment