Created
March 20, 2013 20:24
-
-
Save enile8/5208123 to your computer and use it in GitHub Desktop.
Some in class recursion examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int myFactorial(int integer) | |
{ | |
if(integer == 1) | |
return 1; | |
else | |
{ | |
return (integer * (myFactorial(integer-1))); | |
} | |
} | |
void desend(int counter) | |
{ | |
if(counter != 0) | |
{ | |
cout << counter << "\n"; | |
desend(--counter); | |
} | |
} | |
void ascend(int counter, int stop) | |
{ | |
if(counter != stop) | |
{ | |
cout << counter << "\n"; | |
ascend(++counter, stop); | |
} | |
} | |
int main() | |
{ | |
int fact = myFactorial(3); | |
cout << fact << "\n"; | |
desend(5); | |
cout << "\n"; | |
ascend(1,5); | |
cin.get(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment