Skip to content

Instantly share code, notes, and snippets.

@jesuscmadrigal
Created September 22, 2017 23:08
Show Gist options
  • Save jesuscmadrigal/c1c509e027fda5fc6664511897986046 to your computer and use it in GitHub Desktop.
Save jesuscmadrigal/c1c509e027fda5fc6664511897986046 to your computer and use it in GitHub Desktop.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
// input is a non-negative value
// output is the factorial of n.
// remember that 0! is 1
// fix the wrong answer of always 42
unsigned long long factorial(unsigned long long n){
if (n==0) {
return 1;
} else {
n=n*factorial(n-1);
} return n;
}
// input is a non-negative value
// output is the nth Fibonacci number, thus converting to a function the sequence
// of Fibonacci which stars with zero
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.
// fix the wrong answer of always 42
unsigned long long fibonacci(unsigned long long n){
if (n<2) {
return n;
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
return n;
}
int main(){
for (unsigned int i = 0; i <= 10; i++){
cout << "Factorial of " << i << " is " << factorial(i) << endl;
cout << "Fibonacci " << i << " is " << fibonacci(i) << endl;
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment