Let's say that n is the variable that holds the number to display. The trick here is that in the first loop, n is not the index of the loop. It's some other variable like i. Each time through the loop you multiple n by 3 and print it out.
In addition to http://www.cplusplus.com/forum/unices/204300/#msg974514 I would suggest the first for loop should work something like this: for (int i = n; i < 27; i*= 3). This way the for loop can use different numbers to generate the same concept.