Printing concentric squares of numbers I wrote a code which prints the following pattern :

5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5


The code is as follows : 

#include &lt;stdio.h&gt;

void main(){
   int n,i,k,b,a;
   printf("ENTER OUTER NUMBER:");
   scanf("%d",&amp;n);
   for(i=0;i&lt;2*n-1;i++){
     printf("%d",n);
   }
   printf("\n");
   int p=n;
   for(i=0;i&lt;n-1;i++){
     int a=0;
     for(k=0;k&lt;=i;k++){
        printf("%d",n-a);
        a++;
     }
     for(b=0;b&gt;i;b++){
        printf(" ");
     } 
     for(k=1;k&lt;=2*p-3;k++){
        printf("%d",n-i-1);
     }  
     p--;
     for(k=i;k&gt;=0;k--){
        printf("%d",n-k);
     }
     printf("\n");
   }
    int q=1;
    for(i=0;i&lt;n-2;i++){
     for(k=n;k&gt;i+1;k--){
        printf("%d",k);
     }
     for(k=1;k&lt;=2*q-1;k++){
        printf("%d",q+1);
     }
     q++;
     for(k=2+i;k&lt;=n;k++){
        printf("%d",k);
     }
     printf("\n");
   }
   for(i=0;i&lt;2*n-1;i++){
     printf("%d",n);
   }
   getchar();
}


I printed the first and last line independently.Then,I divided the pattern into two halves and considered some common triangular patterns as follows:

5                 5    2 3 4 5 
5 4             4 5      3 4 5
5 4 3         3 4 5        4 5
5 4 3 2     2 3 4 5          5 


The code ran well in the CODE BLOCKS IDE.But, I think it's too long and I complicated the coding.My query is:


Can I shorten the code with the similar logic of breaking the pattern into parts or mine is ok ?
Is there any alternative to this ?
Use a bit more spaces. It will make your code more readable.
Try to be coherent with your indents
You should sanitize input. What happen if user enter a number greater than 9 ? Or 0, 1, or 2? Or negative number? Or not a number? Never trust user, They all try to broke your program.
When you just print on char, prefer putchar() instead of printf().


Here's my solution, maybe not the most optimal, but clean and short. I compute the distance from the current coordinate to nearest side and then remove it to the base number.

#include &lt;stdio.h&gt;
int main(void) {
    int n = 5;
    int a;
    int b;
    const int m = 2*n-1;
    for (int i = 0; i &lt; m; ++i) {
        for (int j = 0; j &lt; m; ++j) {
            a = (i &gt;= n) ? m - i - 1: i;
            b = (j &gt;= n) ? m - j - 1 : j;
            putchar('0' + n - ((a &lt; b) ? a : b));
        }     
        putchar ('\n');
    }
    return 0;
}


To improve this, you could split the inner loop to avoid the two conditional assignments. I let you trying to do it yourself.