-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path13_pattern.c
107 lines (80 loc) · 2.4 KB
/
13_pattern.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// // C proram to print following Pattern
/*
Pattern 13.
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
*/
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// // Main Function Start
int main()
{
int maxRows, maxCols;
printf("\nHow Many Rows => ");
scanf("%d", &maxRows);
// // Handling Invalid Input
if (maxRows < 1)
{
printf("\n!!! Invalid Input,Plz Enter Positive Number....");
exit(0);
}
// // Determine Number of Columns According to maxRows
maxCols = maxRows * 2 - 1;
// // Print Pattern
puts("\n--------------------------------------------\n");
// // 1st Approach
int spacesInCurrentRow;
for (int row = 1; row <= maxRows; row++)
{
spacesInCurrentRow = (row - 1) * 2 - 1;
for (int col = 1; col <= maxRows + 1 - row; col++)
printf("%c", 'A' - 1 + col);
for (int space = 1; space <= spacesInCurrentRow; space++)
printf(" ");
for (int col = row == 1 ? maxRows - 1 : maxRows + 1 - row; col; col--)
printf("%c", 'A' - 1 + col);
printf("\n");
}
// // 2nd Approach
// // int spacesInCurrentRow;
// // char charAtCol;
// // for (int row = 1; row <= maxRows; row++)
// // {
// // spacesInCurrentRow = (row - 1) * 2 - 1;
// // charAtCol = 'A';
// // for (int col = 1; col <= maxRows + 1 - row; col++)
// // printf("%c", charAtCol++);
// // for (int space = 1; space <= spacesInCurrentRow; space++)
// // printf(" ");
// // charAtCol -= row == 1 ? 2 : 1;
// // for (int col = row == 1 ? maxRows - 1 : maxRows + 1 - row; col; col--)
// // printf("%c", charAtCol--);
// // printf("\n");
// // }
// // 3rd Approach
// // char charAtCol;
// // for (int row = 1; row <= maxRows; row++)
// // {
// // charAtCol = 'A';
// // for (int col = 1; col <= maxCols; col++)
// // {
// // if (col <= maxRows + 1 - row || col >= maxRows - 1 + row)
// // printf("%c", charAtCol);
// // else
// // printf(" ");
// // col < maxRows ? charAtCol++ : charAtCol--;
// // }
// // printf("\n");
// // }
printf("\n");
getch();
return 0;
}
// // Main Function End