Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. // counts to the number provided with even numbers using a recursive recursive algo
  2. // written by Matthew Early, but hey this is a pretty common practice problem
  3. // default compile:
  4. // compile in terminal with 'g++ recursive-even-counter.cpp'
  5. // run with './a.out' in terminal
  6.  
  7. #include <iostream>
  8. #include <cstdlib>
  9.  
  10. using namespace std;
  11.  
  12. void counting(int x);
  13.  
  14. int main() {
  15. int x = 0;
  16. cout << "enter a number to count to evenly: ";
  17. cin >> x;
  18. counting(x);
  19. cout << endl;
  20. }
  21.  
  22.  
  23. void counting(int x) {
  24. if (x == 0) {
  25. cout << 0 << " ";
  26. return 0;
  27. }
  28.  
  29. if (x < 0) { exit(-1); }
  30.  
  31. if (x%2 == 0) {
  32. counting(x-1);
  33. cout << x << " ";
  34. } else { //x is odd
  35. counting(x-1);
  36. }
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement