Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement