0% found this document useful (0 votes)
0 views

Pattern Lab 5

The document contains multiple C programming code snippets that generate various patterns using asterisks and numbers. Patterns include a triangle of asterisks, a triangle with repeating numbers, a left-aligned number pyramid, a specific number pattern, a 'Z' shape, and a hollow rectangle. Each code snippet is self-contained and demonstrates different looping and printing techniques in C.

Uploaded by

jo9ruogh110
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Pattern Lab 5

The document contains multiple C programming code snippets that generate various patterns using asterisks and numbers. Patterns include a triangle of asterisks, a triangle with repeating numbers, a left-aligned number pyramid, a specific number pattern, a 'Z' shape, and a hollow rectangle. Each code snippet is self-contained and demonstrates different looping and printing techniques in C.

Uploaded by

jo9ruogh110
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

keyboard_arrow_down Triangle of Asterisks

#include <stdio.h>

int main() {
int i, j, rows = 5;

for (i = rows; i >= 1; i--) {


for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

keyboard_arrow_down Triangle with Repeating Numbers


#include <stdio.h>

int main() {
int i, j, rows = 5;

for (i = 1; i <= rows; i++) {


for (j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}

return 0;
}

keyboard_arrow_down Left-Aligned NumberPyramid


#include <stdio.h>

int main() {
int i, j, space, rows = 5;

for (i = 1; i <= rows; i++) {


for (space = 1; space <= rows - i; space++) { // Prints spaces for alignment
printf(" ");
}
// Print numbers
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}

return 0;
}

keyboard_arrow_down Patter 5
#include <stdio.h>

int main() {
int i, j;
int rows = 5;

// Upper half of the pattern


for (i = 1; i <= rows; i += 2) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}

// Lower half of the pattern


for (i = rows - 2; i >= 1; i -= 2) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
}
printf("\n");
}

return 0;
}

keyboard_arrow_down 'Z' pattern


#include <stdio.h>

int main() {
int i, j, size = 5; // Change size for a bigger 'Z'

for (i = 0; i < size; i++) {


for (j = 0; j < size; j++) {
if (i == 0 || i == size - 1 || j == (size - 1 - i)) { // The || is logical OR operator for mulaiple code
printf("* ");
} else {
printf(" "); // Print spaces
}
}
printf("\n");
}

return 0;
}

keyboard_arrow_down Hollow Rectangle


#include <stdio.h>

int main() {
int rows = 7, cols = 5; // Modify for different rectangle sizes
int i, j;

// Loop for rows


for (i = 1; i <= rows; i++) {
// Loop for columns
for (j = 1; j <= cols; j++) {

if (i == 1 || i == rows || j == 1 || j == cols) {
printf("* ");
} else {
printf(" "); // Space inside the rectangle
}
}
printf("\n");
}

return 0;
}

You might also like