Implement the following function. 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
as it is mentioned you cannot use loops, the only other option to "imitate" a loop is to use recursion. The following code is in python and considering how python code is quite similar to a code in "english" you might get some hints from it: def pattern(n): print n if (n > 4242): print n return pattern(2*n) print n
I didn't see FoolAround's solution before devising my own, so mine is slightly different. void pattern(int n) { cout << n << endl; if (n < 4242) pattern(2*n); cout << n << endl; }
Oh, that should be "if (n <= 4242)"
Join our real-time social learning platform and learn together with your friends!