A nested for loop is a loop placed inside another for loop. In this program, the outer loop controls the rows, while the inner loop prints the value of the row number repeatedly. As a result, each row contains the same number, and the value changes only when the outer loop moves to the next row.
How It Works?
r and c.
for loop with r = 1.
for loop with c = 1.
r followed by a space.
c <= 5.
#include <stdio.h> #include <conio.h> main() { int r, c; clrscr(); for(r = 1; r <= 5; r++) { for(c = 1; c <= 5; c++) { printf("%d ", r); } printf("\n"); } getch(); }
for loops to print the row number 4 times for 4 rows.r and c. Run both loops from 1 to 4. Print the value of r inside the inner loop and print a new line after each row.#include <stdio.h> #include <conio.h> main() { int r, c; clrscr(); for(r = 1; r <= 4; r++) { for(c = 1; c <= 4; c++) { printf("%d ", r); } printf("\n"); } getch(); }
for loop controls the row number, while the inner for loop prints the value of r four times in each row. After completing one row, a new line is printed, and the process repeats until all four rows are displayed.