Write a nested for loop that prints the following output: 1 1 2 1 1 2 4 2 1 1 2
ID: 3621866 • Letter: W
Question
Write a nested for loop that prints the following output:1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Hint: Here is the psudocode solution:
for row from 0 to 7 {
pad leading blanks in a row like this
for the column from 1 to 7-row:
print a set of spaces
print left half of row
for column from 0 to row:
print spaces followed by 2 raised to the power of the column
print right half of row
for column from row-1 to 0:
print spaces followed by 2 raised to the power of the column
print a new line
You need to figure out how many spaces to print before the number. The simple way to compute powers of 2 is with the pow function; can you do this without it?
Explanation / Answer
#include int main() { int i,j,k=0; for(i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.