forked from tech-hima/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare printing pattern.txt
50 lines (39 loc) · 1.16 KB
/
square printing pattern.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
int main()
{
int n , i ,j,p ;
scanf("%d",&n);
for(i=1; i<=(n) ;i++) /// this loop is to print the upper triangle for n rows
{
printf("\n");
for(j=1; j<=i ;j++){
printf("%d ",(n-j+1));
p=(n-j+1);
}
for(j=i+1;j<=2*n-i;j++){ // repeat the last element till 1st, 2nd last etc column
printf("%d ",p);
}
p=(p+1);
for(j=2*n-i+1; j<=2*n-1 ;j++){
printf("%d ",p);
p += 1;
}
}
for(i=n+1; i<=(2*n-1) ;i++) // this loop is to print the lower triangle in decreasing order for last n-1 rows
{
printf("\n");
for(j=1; j<=2*n-i ;j++){
printf("%d ",(n-j+1));
p=(n-j+1);
}
for(j=2*n-i+1;j<=i ;j++){ // repeat the last element till i equals j
printf("%d ",p);
}
p=(p+1);
for(j=i+1; j<=2*n-1 ;j++){
printf("%d ",p);
p += 1;
}
}
return 0;
}