#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void printPattern(int n) {
int size = 2 * n - 1;
int matrix[size][size];
int startRow = 0, endRow = size - 1;
int startCol = 0, endCol = size - 1;
int num = n;
while (startRow <= endRow && startCol <= endCol) {
for (int i = startCol; i <= endCol; i++) {
matrix[startRow][i] = num;
}
startRow++;
for (int i = startRow; i <= endRow; i++) {
matrix[i][endCol] = num;
}
endCol--;
if (startRow <= endRow) {
for (int i = endCol; i >= startCol; i--) {
matrix[endRow][i] = num;
}
endRow--;
}
if (startCol <= endCol) {
for (int i = endRow; i >= startRow; i--) {
matrix[i][startCol] = num;
}
startCol++;
}
num--;
}
// Print the pattern
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int n;
scanf("%d", &n);
printPattern(n);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: