Homework – Nested Loop
1. Trace the following programs. Assume that SIZE has the value 5.
Program Memory Output
for (int i = 1; i < SIZE; i++) {
for (int j = SIZE; j >= i; j--)
{
[Link](“*”);
}
[Link]();
}
2. Write a program that draws a rectangle with stars, given the number of rows
and columns.
Sample Input Sample Output
Enter # of rows: 5 ****
Enter # of columns: 4 ****
****
****
3. Write a program that will prompt user for two numbers x & y then output a
table of numbers with x rows and each of the rows lists number from 1 to y,
separated by a space.
Sample Input Sample Output
Enter x: 5 1 2 3 4 5 6
Enter y: 6 1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
4. Write a program using nested loops that prints the following pattern.
1
12
123
1234
12345
5. Three positive integers a, b, and c with a < b < c form a Pythagorean triplet if
a2 + b2 = c2. For example 3, 4, 5 form a Pythagorean triplet since 3 2 + 42 = 52.
Write a program that first prompts the user for a positive integer and then
finds and prints all Pythagorean triplets whose largest member is less than or
equal to that integer.