Implement the following function using recursion. Do not use any local variables
ID: 674699 • 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(unsigned int n)
{
cout << n << endl;
if(n>4242)
{
cout << n << endl;
return;
}
else
{
pattern(2*n);
cout << n << endl;
}
}
int main()
{
pattern(840);
return 0;
}