Created
November 6, 2019 20:16
-
-
Save ajinkyajawale14499/5ccd81a9ef3e44f69c934ea8f51a6705 to your computer and use it in GitHub Desktop.
number pattern printing with matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void) | |
{ | |
int m,n; | |
//accept the number 'n' from the user | |
printf ("ENTER the value of n"); | |
printf ("\n"); | |
scanf("%d",&n); | |
m=n; | |
int a[n][n]; | |
int i,j,k,l; | |
// for initializing all elements in the matrix as 0 | |
for(i=0;i<m;i++) | |
for(j=0;j<n;j++) | |
a[i][j]=0; | |
i=1; | |
for(j=0;j<m-j;j++) | |
{ | |
if(i<((m*n)+1)) | |
{ | |
//print rows from top | |
for(k=j;k<n-j;k++) | |
if(a[j][k]==0) | |
a[j][k]=i++; | |
k=k-1; | |
//print columns from left hand side | |
for(l=j+1;l<m-j;l++) | |
if(a[l][k]==0) | |
a[l][k]=i++; | |
l=l-2; | |
//print rows from bottom | |
while(l>j) | |
if(a[k][l]==0) | |
a[k][l--]=i++; | |
//print columns from right hand side | |
while(k>j) | |
if(a[k][j]==0) | |
a[k--][j]=i++; | |
} | |
} | |
// for printing the matrix | |
for(i=0;i<m;i++) | |
{ | |
for(j=0;j<n;j++) | |
{ | |
printf("%d\t",a[i][j]); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OUTPUT:
ENTER the value of n: 8
1 2 3 4 5 6 7 8
28 29 30 31 32 33 34 9
27 48 49 50 51 52 35 10
26 47 60 61 62 53 36 11
25 46 59 64 63 54 37 12
24 45 58 57 56 55 38 13
23 44 43 42 41 40 39 14
22 21 20 19 18 17 16 15