-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple Pyramid Patterns.c
244 lines (229 loc) · 7.76 KB
/
Simple Pyramid Patterns.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h> // Standard input-output header
#include<conio.h> // Console input-output header (optional for modern compilers)
// SIMPLE PYRAMID PATTERNS
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function prototypes for different patterns
void pattern_1();
void pattern_2();
void pattern_3();
void pattern_4();
void pattern_5();
void pattern_6();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int choice; // Variable to store the user's choice of pattern
// Display options to the user
printf("Enter 1 to print pattern number 1.\n");
printf("Enter 2 to print pattern number 2.\n");
printf("Enter 3 to print pattern number 3.\n");
printf("Enter 4 to print pattern number 4.\n");
printf("Enter 5 to print pattern number 5.\n");
printf("Enter 6 to print pattern number 6.\n\n");
// Prompt the user for their choice
printf("Enter choice: ");
scanf("%d", &choice);
// Use switch-case to call the appropriate pattern function based on user input
switch(choice)
{
case 1:
pattern_1(); // Call function to print pattern 1
break;
case 2:
pattern_2(); // Call function to print pattern 2
break;
case 3:
pattern_3(); // Call function to print pattern 3
break;
case 4:
pattern_4(); // Call function to print pattern 4
break;
case 5:
pattern_5(); // Call function to print pattern 5
break;
case 6:
pattern_6(); // Call function to print pattern 6
break;
default:
printf("Wrong Input.............!!!"); // Handle invalid choices
break;
}
return 0; // Indicate successful termination of the program
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void pattern_1()
{
// Pattern:
// 1
// 2 2
// 3 3 3
// 4 4 4 4
// 5 5 5 5 5
for(int i = 1; i <= 5; i++) // Loop through rows
{
for(int j = 1; j <= i; j++) // Loop to print the number 'i'
{
printf(" %d", i); // Print current row number 'i'
}
printf("\n"); // Move to the next line after each row
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void pattern_2()
{
// Pattern:
// 5 5 5 5 5
// 4 4 4 4
// 3 3 3
// 2 2
// 1
for(int i = 5; i >= 1; i--) // Loop through rows in reverse
{
for(int j = i; j > 0; j--) // Loop to print the number 'i'
{
printf(" %d", i); // Print current row number 'i'
}
printf("\n"); // Move to the next line after each row
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void pattern_3()
{
// Pattern:
// 1
// 2 2 2
// 3 3 3 3 3
// 4 4 4 4 4 4 4
// 5 5 5 5 5 5 5 5 5
int start = 5, stop = 5; // Initialize start and stop for printing
for(int i = 1; i <= 5; i++) // Loop through rows
{
for(int j = 1; j <= 9; j++) // Loop to manage the total width
{
if(j >= start && j <= stop) // Check if within the range to print the number
{
printf(" %d ", i); // Print current row number 'i'
}
else
{
printf(" "); // Print spaces for alignment
}
}
printf("\n"); // Move to the next line after each row
start--; // Adjust start for next row
stop++; // Adjust stop for next row
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void pattern_4()
{
// Pattern:
// 1
// 2 2
// 3 3 3
// 4 4 4 4
// 5 5 5 5 5
// 4 4 4 4
// 3 3 3
// 2 2
// 1
for(int i = 1; i <= 5; i++) // Loop for the first half
{
for(int j = 1; j <= i; j++) // Loop to print the number 'i'
{
printf("%d ", i); // Print current row number 'i'
}
printf("\n"); // Move to the next line after each row
}
for(int i = 4; i >= 1; i--) // Loop for the second half
{
for(int j = 1; j <= i; j++) // Loop to print the number 'i'
{
printf("%d ", i); // Print current row number 'i'
}
printf("\n"); // Move to the next line after each row
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void pattern_5()
{
// Pattern:
// *
// * *
// * *
// * *
// *********
int cursor1 = 5, cursor2 = 5; // Initialize cursor positions for stars
for(int i = 1; i <= 5; i++) // Loop through rows
{
for(int j = 1; j <= 9; j++) // Loop to manage the total width
{
if((j == cursor1 || j == cursor2) && i < 5) // Check for star positions
{
printf("*"); // Print star
}
else if(i == 5) // For the last row
{
printf("*"); // Print stars in the last row
}
else
{
printf(" "); // Print spaces for alignment
}
}
printf("\n"); // Move to the next line after each row
cursor1++; // Adjust cursor for left star position
cursor2--; // Adjust cursor for right star position
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void pattern_6()
{
// Pattern:
// 1
// 2 2 2
// 3 3 3 3 3
// 4 4 4 4 4 4 4
// 5 5 5 5 5 5 5 5 5
// 4 4 4 4 4 4 4
// 3 3 3 3 3
// 2 2 2
// 1
int start1 = 5, stop1 = 5, start2 = 2, stop2 = 8; // Initialize start and stop positions for the first and second halves
for(int i = 1; i <= 5; i++) // Loop for the upper half
{
for(int j = 1; j <= 9; j++) // Loop to manage the total width
{
if(j >= start1 && j <= stop1) // Check if within the range to print the number
{
printf(" %d ", i); // Print current row number 'i'
}
else
{
printf(" "); // Print spaces for alignment
}
}
printf("\n"); // Move to the next line after each row
start1--; // Adjust start for next row
stop1++; // Adjust stop for next row
}
for(int i = 4; i >= 1; i--) // Loop for the lower half
{
for(int j = 1; j <= 9; j++) // Loop to manage the total width
{
if(j >= start2 && j <= stop2) // Check if within the range to print the number
{
printf(" %d ", i); // Print current row number 'i'
}
else
{
printf(" "); // Print spaces for alignment
}
}
printf("\n"); // Move to the next line after each row
start2++; // Adjust start for next row
stop2--; // Adjust stop for next row
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////