C++ Use recursion to complete the function oddDownEvenUp() so it first prints al
ID: 660939 • Letter: C
Question
C++
Use recursion to complete the function oddDownEvenUp() so it first prints all odd integers from largest odd number <= argument down to 1. The same function call must then print all the even integers in the same range of 2 through the largest even integers <= argument. The following function calls shown to the left must generate the output shown in comments in the right. Do not use any loop anywhere, use recursion.
oddDownEvenUp(1); // 1
oddDownEvenUp(2); // 1 2
oddDownEvenUp(4); // 3 1 2 4
oddDownEvenUp(9); // 9 7 5 3 1 2 4 6 8
Explanation / Answer
int oddDownEvenUp(int n){
if (n > 0)
{
if (n % 2 == 1) cout << n << " ";
oddDownEvenUp(n-1);
if(n % 2 == 0) cout << n << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.