Pattern Generation Lab Report
Pattern Generation Lab Report
Objective
To implement two C programs that generate specific patterns:
1. A symmetric pyramid of numbers.
2. A hollow diamond pattern using asterisks (*).
Background Study
Pattern generation is a common exercise in programming to understand the usage of nested
loops, conditional statements, and logic development.
- Nested Loops: Used to control rows and columns of the pattern.
- Conditionals: Help manage spaces and the placement of characters.
- Iteration Logic: Determines the sequence and arrangement of numbers or symbols.
Algorithm
Code Snippet
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter no of lines = ");
scanf("%d", &n);
// Top half
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) printf("%d ", j); // Ascending
for (j = i - 1; j >= 1; j--) printf("%d ", j); // Descending
printf("\n");
}
// Bottom half
for (i = n - 1; i >= 1; i--) {
for (j = 1; j <= i; j++) printf("%d ", j); // Ascending
for (j = i - 1; j >= 1; j--) printf("%d ", j); // Descending
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int n, i, j, space;
printf("Enter value of n: ");
scanf("%d", &n);
// Upper triangle
for (i = 1; i <= n; i++) {
for (space = 1; space <= n - i; space++) printf(" ");
for (j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) printf("*");
else printf(" ");
}
printf("\n");
}
// Lower triangle
for (i = n - 1; i >= 1; i--) {
for (space = 1; space <= n - i; space++) printf(" ");
for (j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) printf("*");
else printf(" ");
}
printf("\n");
}
return 0;
}
2. Hollow Diamond:
- Spaces control indentation.
- Conditional statements ensure the pattern remains hollow, printing * only at the edges.
Output Snippet
Enter no of lines = 7
1
121
12321
1234321
123454321
12345654321
1234567654321
12345654321
123454321
1234321
12321
121
1
Enter value of n: 7
*
**
* *
* *
* *
* *
* *
* *
* *
* *
* *
**
*
Conclusion
The C programs successfully generate the desired patterns using nested loops and
conditional statements. This exercise enhances the understanding of loop structures and
logic in pattern generation.