Nested for loops are used to print different types of patterns. A nested loop means one for loop is placed inside another for loop. The outer loop controls the number of rows, while the inner loop controls what is printed in each row. By changing the starting value, ending value, or the condition of the inner loop, we can create a variety of number patterns.
How It Works?
r and c.
for loop to control the number of rows.
for loop to control the numbers printed in each row.
printf("\n");.
#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 ", c); } printf("\n"); } getch(); }
#include <stdio.h> #include <conio.h> main() { int r, c; clrscr(); for(r = 1; r <= 5; r = r + 1) { for(c = 1; c <= r; c = c + 1) { printf("%d ", c); } printf("\n"); } getch(); }
#include <stdio.h> #include <conio.h> main() { int r, c; clrscr(); for(r = 1; r <= 5; r++) { for(c = 5; c >= r; c--) { printf("%d ", c); } printf("\n"); } getch(); }
#include <stdio.h> #include <conio.h> main() { int r, c; clrscr(); for(r = 5; r >= 1; r--) { for(c = 1; c <= r; c++) { printf("%d ", r); } printf("\n"); } getch(); }
for loops to print the following pattern.1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
for loops can be used to print a pattern by controlling rows and repeating the row number using the inner loop.r and c. Run the outer loop from 1 to 5. Run the inner loop from 1 to r and print the value of r inside the inner loop.#include <stdio.h> #include <conio.h> main() { int r, c; clrscr(); for(r = 1; r <= 5; r++) { for(c = 1; c <= r; c++) { printf("%d ", r); } printf("\n"); } getch(); }
for loops to create a number pattern. The outer loop controls the rows, while the inner loop repeats the current row number according to the row value. After completing each row, a new line is printed to display the next pattern row.