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

Pattern Codes

Uploaded by

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

Pattern Codes

Uploaded by

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

Pattern 1:

#include <stdio.h>
#include <time.h>

int main() {
time_t tm;
time(&tm);
printf("Current Date/Time = %s\n\n\n", ctime(&tm));

int i, j, num = 1;

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


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

return 0;
}
Output:
1
23
456
7 8 9 10
Pattern 2:
#include <stdio.h>
#include <time.h>

int main() {
time_t tm;
time(&tm);
printf("Current Date/Time = %s\n\n\n", ctime(&tm));

int n = 4; // Number of rows


int i, j, spaces;

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


// Print increasing numbers
for (j = 1; j <= i; j++) {
printf("%d", j);
}
spaces = 2 * (n - i) - 1; // Decreasing spaces for each row
for (j = 1; j <= spaces; j++) {
printf(" ");
}

if (i != n) {
for (j = i; j >= 1; j--) {
printf("%d", j);
}
} else {
for (j = i - 1; j >= 1; j--) {
printf("%d", j);
}
}

printf("\n");
}

return 0;
}

Output:
1 1
12 21
123 321
1234321
Pattern 3:
#include <stdio.h>
#include <time.h>

int main() {
time_t tm;
time(&tm);
printf("Current Date/Time = %s\n\n\n", ctime(&tm));

int i, j, n = 4;

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


// Print leading spaces
for (j = 0; j < i; j++) {
printf(" ");
}

// Print increasing numbers


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

// Print decreasing numbers


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

return 0;
}
Output:
1234321
12321
121
1
Pattern 4:
#include <stdio.h>
#include <time.h>

int main() {
time_t tm;
time(&tm);
printf("Current Date/Time = %s\n\n\n", ctime(&tm));

int n = 4; // Peak number in the pattern


int i, j, spaces;

// Upper part of the pattern


for (i = 1; i <= n; i++) {
// Print increasing numbers
for (j = 1; j <= n - i + 1; j++) {
printf("%d", j);
}

// Calculate and print spaces


spaces = 2 * (i - 1) - 1;
for (j = 1; j <= spaces; j++) {
printf(" ");
}

// Print decreasing numbers if it's not the first row (no spaces in the top row)
if (i != 1) {
for (j = n - i + 1; j >= 1; j--) {
printf("%d", j);
}
}

printf("\n");
}

// Lower part of the pattern


for (i = n - 1; i >= 1; i--) {
// Print increasing numbers
for (j = 1; j <= n - i + 1; j++) {
printf("%d", j);
}

// Calculate and print spaces


spaces = 2 * (i - 1) - 1;
for (j = 1; j <= spaces; j++) {
printf(" ");
}

// Print decreasing numbers if it's not the first row of the lower part
if (i != 1) {
for (j = n - i + 1; j >= 1; j--) {
printf("%d", j);
}
}

printf("\n");
}

return 0;
}
Output:
1234321
123 321
12 21
1 1
12 21
123 321
1234321

You might also like