I. Pattern Display Programs
1. Write C programs to display the following patterns using nested loop construct.
Pattern a:
1
1 1
1 1 1
1 1 1 1
#include <stdio.h>
int main() {
int i, j, rows = 4;
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("1 ");
}
printf("\n");
}
return 0;
}
Pattern b:
1
2 2
3 3 3
4 4 4 4
#include <stdio.h>
int main() {
int i, j, rows = 4;
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}
Pattern c:
1
1 2
1 2 3
1 2 3 4
#include <stdio.h>
int main() {
int i, j, rows = 4;
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Pattern d:
4
3 3
2 2 2
1 1 1 1
#include <stdio.h>
int main() {
int i, j, rows = 4;
for(i = rows; i >= 1; i--) {
for(j = rows; j >= i; j--) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}
Pattern e:
4
4 3
4 3 2
4 3 2 1
#include <stdio.h>
int main() {
int i, j, rows = 4;
for(i = rows; i >= 1; i--) {
for(j = rows; j >= i; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Pattern f:
1 2 3 4
1 2 3
1 2
1
#include <stdio.h>
int main() {
int i, j, rows = 4;
for(i = rows; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
II. Dynamic Pattern Programs
2. Modify the solution of question no. 1 to accept the number of lines as the input. The program should make the display pattern accordingly (Hint: write separate programs).
Here's a dynamic version of Pattern a that accepts the number of lines as input:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("1 ");
}
printf("\n");
}
return 0;
}
Similarly, you can modify other patterns by adding user input for the number of rows.
3. Extend the programs of Example 5.6 and Example 5.7 to make it dynamic by accepting the number of lines as an input from the keyboard.
Assuming Example 5.6 and 5.7 are similar pattern programs, here's a general approach to make them dynamic:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Pattern similar to Example 5.6 (increasing numbers)
printf("Pattern 1:\n");
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
printf("\nPattern 2:\n");
// Pattern similar to Example 5.7 (decreasing numbers)
for(i = rows; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
III. Theory Questions
4. What is a nested loop? Why do we use nested loops in our programs?
Nested Loop: A nested loop is a loop inside another loop. The inner loop executes completely for each iteration of the outer loop.
Why we use nested loops:
- To work with multi-dimensional data structures like 2D arrays and matrices
- To generate patterns and sequences
- To implement sorting and searching algorithms
- To process tabular data
- To solve problems that require multiple levels of iteration
- To create complex computational patterns
Example:
for(int i = 0; i < 3; i++) { // Outer loop
for(int j = 0; j < 3; j++) { // Inner loop
printf("(%d, %d) ", i, j);
}
printf("\n");
}
Output: (0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2)
5. Do we need to use same type of loops as outer and inner loops? Justify your answer with some code segments.
No, we don't need to use the same type of loops as outer and inner loops. We can mix different types of loops (for, while, do-while) based on the requirements of our program.
Example 1: for loop inside while loop
#include <stdio.h>
int main() {
int i = 1;
while(i <= 3) { // Outer while loop
printf("Row %d: ", i);
for(int j = 1; j <= 3; j++) { // Inner for loop
printf("%d ", j);
}
printf("\n");
i++;
}
return 0;
}
Example 2: while loop inside for loop
#include <stdio.h>
int main() {
for(int i = 1; i <= 3; i++) { // Outer for loop
printf("Row %d: ", i);
int j = 1;
while(j <= 3) { // Inner while loop
printf("%d ", j);
j++;
}
printf("\n");
}
return 0;
}
Example 3: do-while loop inside for loop
#include <stdio.h>
int main() {
for(int i = 1; i <= 3; i++) { // Outer for loop
printf("Row %d: ", i);
int j = 1;
do { // Inner do-while loop
printf("%d ", j);
j++;
} while(j <= 3);
printf("\n");
}
return 0;
}
6. Can we put a third loop inside the inner loop of a nested loop construct? Write a C program to justify your answer.
Yes, we can put a third loop inside the inner loop of a nested loop construct. This creates a triple nested loop, which is useful for working with 3D arrays or solving problems that require three levels of iteration.
Example Program:
#include <stdio.h>
int main() {
int i, j, k;
printf("Triple nested loop example:\n");
for(i = 1; i <= 2; i++) { // Outer loop
for(j = 1; j <= 2; j++) { // Middle loop
for(k = 1; k <= 2; k++) { // Inner loop
printf("(%d, %d, %d) ", i, j, k);
}
printf("\n");
}
printf("\n");
}
return 0;
}
Output:
(1, 1, 1) (1, 1, 2)
(1, 2, 1) (1, 2, 2)
(2, 1, 1) (2, 1, 2)
(2, 2, 1) (2, 2, 2)
Explanation: The inner loop (k) runs completely for each iteration of the middle loop (j), and the middle loop runs completely for each iteration of the outer loop (i). This creates a total of 2 × 2 × 2 = 8 iterations.