Created
April 24, 2017 19:43
-
-
Save mattearly/78d669877b5fed45f4dc040c7f7705db to your computer and use it in GitHub Desktop.
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
// counts to the number provided with even numbers using a recursive recursive algo | |
// written by Matthew Early, but hey this is a pretty common practice problem | |
// default compile: | |
// compile in terminal with 'g++ recursive-even-counter.cpp' | |
// run with './a.out' in terminal | |
#include <iostream> | |
#include <cstdlib> | |
using namespace std; | |
void counting(int x); | |
int main() { | |
int x = 0; | |
cout << "enter a number to count to evenly: "; | |
cin >> x; | |
counting(x); | |
cout << endl; | |
} | |
void counting(int x) { | |
if (x == 0) { | |
cout << 0 << " "; | |
return 0; | |
} | |
if (x < 0) { exit(-1); } | |
if (x%2 == 0) { | |
counting(x-1); | |
cout << x << " "; | |
} else { //x is odd | |
counting(x-1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment