We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int main()
{
int n;
scanf("%d", &n);
int size = (2 * n) - 1;
int out[size][size];
int i, j;
// Fill the matrix with the concentric layer numbers
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
// Calculate the minimum distance to any edge
int p = i < size - i - 1 ? i : size - i - 1;
p = p < (j < size - j - 1 ? j : size - j - 1) ? p : (j < size - j - 1 ? j : size - j - 1);
out[i][j] = n - p;
}
}
// Print the matrix with space-separated values
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%d ", out[i][j]);
}
printf("\n");
}
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Printing Pattern Using Loops
You are viewing a single comment's thread. Return to all comments →
include
int main() { int n; scanf("%d", &n); int size = (2 * n) - 1; int out[size][size]; int i, j;
}