0% found this document useful (0 votes)
15 views4 pages

Pattern Generation Lab Report

The document outlines a lab report on generating patterns using C programming, specifically a symmetric pyramid of numbers and a hollow diamond pattern. It details the algorithms, code snippets, and the logic behind using nested loops and conditionals for pattern generation. The conclusion emphasizes the educational value of the exercise in understanding programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Pattern Generation Lab Report

The document outlines a lab report on generating patterns using C programming, specifically a symmetric pyramid of numbers and a hollow diamond pattern. It details the algorithms, code snippets, and the logic behind using nested loops and conditionals for pattern generation. The conclusion emphasizes the educational value of the exercise in understanding programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Report: Pattern Generation Using C Programming

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

For the Pyramid of Numbers:


1. Input the number of lines n.
2. Loop through rows from 1 to n.
3. For each row, print ascending numbers (1 to row number).
4. Print descending numbers (row number to 1).
5. After the middle row, repeat the pattern in reverse order.

For the Hollow Diamond:


1. Input the value of n.
2. Loop through n rows to create the upper triangle:
- Print spaces and asterisks based on the current row index.
3. Loop through another n rows for the lower triangle, reversing the logic.
4. Use conditionals to leave inner spaces empty.

Code Snippet

Code for Pyramid of Numbers

#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;
}

Code for Hollow Diamond

#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;
}

Description of Code Snippet


1. Pyramid of Numbers:
- Loops manage ascending and descending parts.
- Separate loops for top and bottom halves to ensure symmetry.

2. Hollow Diamond:
- Spaces control indentation.
- Conditional statements ensure the pattern remains hollow, printing * only at the edges.

Output Snippet

Output for Pyramid of Numbers:

Enter no of lines = 7
1
121
12321
1234321
123454321
12345654321
1234567654321
12345654321
123454321
1234321
12321
121
1

Output for Hollow Diamond:

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.

You might also like