Nested loops refer to including one or more loops within a loop body. Through nested loops, we can handle more complex tasks, such as printing shapes, matrices, tables, etc. Below are several common examples demonstrating how to use nested loops to print rectangles, right-angled triangles, and the multiplication table.
1. Printing a Rectangle
To print a rectangle, we can use nested loops. The outer loop controls the number of rows, while the inner loop controls the number of characters in each row. Suppose we want to print a rectangle with a width of 5 and a height of 7.
Example Code:
#include <stdio.h>
int main() {
int i, j;
int rows = 5, cols = 7; // Number of rows and columns of the rectangle
for (i = 0; i < rows; i++) { // Control rows
for (j = 0; j < cols; j++) { // Control columns
printf("*"); // Print asterisk
}
printf("\n"); // New line after each row
}
return 0;
}
Output:
*******
*******
*******
*******
*******
Explanation:The outer loop controls the number of rows (5 rows), and the inner loop controls the number of characters per row (7 columns), printing one <span>*</span> each time, followed by a new line.
2. Printing a Right-Angled Triangle
To print a right-angled triangle, we need to print the corresponding number of asterisks based on the number of rows. Suppose we print a right-angled triangle with 5 rows.
Example Code:
#include <stdio.h>
int main() {
int i, j;
int rows = 5; // Number of rows in the triangle
for (i = 1; i <= rows; i++) { // Control rows
for (j = 1; j <= i; j++) { // Print i asterisks for each row
printf("*");
}
printf("\n"); // New line after each row
}
return 0;
}
Output:
*
**
***
****
*****
Explanation:The outer loop controls the number of rows (5 rows), and the inner loop controls the number of asterisks printed per row. The first row prints 1 asterisk, the second row 2 asterisks, and so on.
3. Printing an Inverted Right-Angled Triangle
The logic for printing an inverted right-angled triangle is that the first row prints the most asterisks, and as the number of rows increases, the number of asterisks decreases. Suppose we print an inverted right-angled triangle with 5 rows.
Example Code:
#include <stdio.h>
int main() {
int i, j;
int rows = 5; // Number of rows in the triangle
for (i = rows; i >= 1; i--) { // Control rows, from maximum to 1
for (j = 1; j <= i; j++) { // Print i asterisks for each row
printf("*");
}
printf("\n"); // New line after each row
}
return 0;
}
Output:
*****
****
***
**
*
Explanation:The outer loop controls the number of rows (5 rows), and the inner loop controls the number of asterisks printed per row. The first row prints 5 asterisks, the second row 4 asterisks, and so on.
4. Printing the Multiplication Table
The multiplication table is a classic application of nested loops. In this table, the product of the row number and column number is displayed in the corresponding position of the table.
Example Code:
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 9; i++) { // Outer loop controls rows
for (j = 1; j <= i; j++) { // Inner loop controls columns
printf("%d*%d=%2d ", j, i, i * j); // Print multiplication expression
}
printf("\n"); // New line after each row
}
return 0;
}
Output:
1*1= 1
1*2= 2 2*2= 4
1*3= 3 2*3= 6 3*3= 9
1*4= 4 2*4= 8 3*4=12 4*4=16
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
Explanation:The outer loop controls the number of rows (1 to 9), and the inner loop controls the number of columns (1 to the current row number). Each row prints the results of the multiplication table, up to the maximum value <span>9*9</span>。
5. Summary
- • Nested Loops:Involves including another loop within a loop body to handle more complex tasks, such as drawing shapes, outputting tables, etc.
- • Printing a Rectangle:The outer loop controls the number of rows, and the inner loop controls the number of characters per row.
- • Printing a Right-Angled Triangle:The outer loop controls the number of rows, and the inner loop prints the corresponding number of asterisks based on the row number.
- • Printing an Inverted Right-Angled Triangle:The outer loop controls the number of rows, and the inner loop prints asterisks based on the remaining rows.
- • Printing the Multiplication Table:The outer loop controls the number of rows, and the inner loop prints the multiplication table based on the current row number.