Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the following function using recursion. Do not use any local variables

ID: 3757774 • Letter: I

Question

Implement the following function using recursion. Do not use any local variables or loops.

  
void pattern(unsigned int n)

   // Precondition: n > 0;

   // Postcondition: The output consists of lines of integers. The first line

   // is the number n. The next line is the number 2n. The next line is

   // the number 4n, and so on until you reach a number that is larger than

   // 4242. This list of numbers is then repeated backward until you get back

   // to n.

   /* Example output with n = 840:

   840

   1680

   3360

   6720

   6720

   3360

   1680

   840 */

Explanation / Answer

#include <iostream>

using namespace std;

void pattern(int n)

{

if(n>4242)

{

cout << n << endl;

cout << n << endl;

return;

}

else

{

cout << n << endl;

pattern(2*n);

cout << n << endl;

}

}

int main()

{

pattern(840);

return 0;

}