CHAPTERFOUR
4-5. Number Stack Same. Write a program that uses a nested loop to generate the output shown below. Be sure that all the values on a single line are the same. For example, line 5 has the values “5”, “5”, “5”, “5”, and “5”.
MEI a
2 3 3 4 4 4 5 5 5 5 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9
O
4-6. Right Triangle. Write a program that generates the following triangle to console output. Note that the number of blank spaces decreases each line. (Hint: Modify the solution in Figure 4-14 by adding a “SpaceCount” loop above the “CircleCount” loop. Both the SpaceCount and CircleCount loops should be nested inside the LineCount loop.)
PO 00 000 0000 00 00 0 000 000 0000 000 00000000 000000000 000000 0 00 0
1.
.6
4-7. Tree. Write a program that generates the following tree shape to console output. Note that every line contains an odd number of circles.
0 000 0 00 0 0 0 0 000 0 0 000000 0 0 0 000 000 0 0 0 0 0 000 0 0 000 0 0 0 0 0 0000 0 000 0 0 0 0 0 0 0 00000 0 0 0 0 0 0 0 0 0 0 0 0 00000000000 00000 0 0 0
1011
17 • 67
■
FOR LOOPS AND NESTED LOOPS
4-8. Diamond. Write a program that generates the following diamond shape to console output. Note that every line has an odd number of circles.
0 000 0 0 0 0 0 0 0 0 0 0 0 0 00 0 00 0 0 C 0 0 0 00 0 0 0 0 C 00 00 00 0 0 0 0 0 C 0 00 000 00 0 0 0 0 0 C 0 00 0 000 0 0 0 0 0 0 0 0 C 0 00 00 0 0000 0 0 0 0 0 0 0 C 0 0000 0 0000 00 0 0 0 0 0 C 000 00 1 0000 0 0 0 0 0 0 C 0 00 0 00 0 0 0 0 0 0 0 C 0 00 00 0 0 0 0 0 0 C 0 0 000 0 0 0 0 C 0 0 0 000 0 0 0 0 0 0 0 000 0
.1/
71 72 Copyright 2009 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Copyright 2009 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.