-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path09_swastika_pattern.cpp
213 lines (166 loc) · 5.9 KB
/
09_swastika_pattern.cpp
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
/*
卐 Swastika Pattern
Take as input N, an odd number (>=5) . Print the following pattern as given below for N = 7.
* ****
* *
* *
*******
* *
* *
**** *
Input Format: Enter value of N ( >=5 )
Constraints: 5 <= N <= 99
Output Format: Print the required pattern.
Sample Input: 7
Sample Output: * ****
* *
* *
*******
* *
* *
**** *
Explanation: Catch the pattern for the corresponding input and print it accordingly.
*/
#include<iostream>
using namespace std;
int main() {
int total_rows;
cin >> total_rows;
int total_cols = total_rows;
int mid= (total_rows/2)+1;
int last = total_rows;
for(int row=1; row<=total_rows; row++){
for(int col=1; col <= total_cols; col++){
if((col==1 && row<mid) || (col==last && row>mid) || col==mid || row==mid || (row==1 && col>mid) || (row==last && col<mid) )
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}
/*
Divide the given pattern in two parts from middle .Now do the work of these parts separately and print the required pattern accordingly.
Code:
#include <iostream>
using namespace std;
int main (){
int row, col;
int total_rows;
cin >> total_rows;
// work for first row
cout << "*";
for (col = 1; col <= (total_rows+1)/2 - 2; col++)
cout << " ";
for (col = 1; col <= (total_rows+1)/2; col++)
cout << "*";
cout << endl;
//work till middle row
for (row = 1; row <= (total_rows+1)/2 - 2; row++)
{
cout << "*";
for (col = 1; col <= (total_rows+1)/2 - 2; col++)
cout << " ";
cout << "*";
cout << endl;
}
//work for middle row
for (row = 1; row <= total_rows; row++)
cout <<"*";
cout << endl;
//work till last row
for (row = 1; row <= (total_rows+1)/2 - 2; row++)
{
for (col = 1; col <= (total_rows+1)/2 - 1; col++)
cout << " ";
cout << "*";
for (col = 1; col <= (total_rows+1)/2 - 2; col++)
cout << " ";
cout << "*";
cout << endl;
}
//work of last row
for (row = 1; row <= (total_rows+1)/2; row++)
cout << "*";
for (row = 1; row <= (total_rows+1)/2 - 2; row++)
cout << " ";
cout <<"*";
return 0;
}
Lang - Java
Short Info about the variables:-
1. nsp (number of spaces)-> Number of spaces in very First Line of the pattern.
2. nst (number of stars)-> Number of stars in very first line of the pattern.
3. csp (counter of spaces)-> counter of spaces that will print the required number of spaces and will be initialized with 1 and incremented upto nsp.
4. cst (counter of stars)-> counter of spaces that will print the required number of stars and will be initialized with 1 and incremented upto nst.
5. rows -> It will be initialized with 1 and will go upto the total number of rows in the pattern.
Given pattern is seen as first we need to work for spaces and then for the numbers in each row.
Numbers are first increasing till the mid element and then decreasing till end of the row.
Thus, do the work of first spaces and then for numbers .
And then change variables accordingly for next iterations.
Always think of first row and then work for the next iteration and repeat the work total number of row times. That's how you can solve any problem related to pattern.
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
PattenG(n);
}
public static void PattenG(int n) {
int nsp = n / 2 - 1;
int rows = 1;
int nst = n / 2;
while (rows <= n) {
if (rows <= n / 2 + 1) {
if (rows <= n / 2) {
System.out.print("*");
int csp = 1;
while (csp <= nsp) {
System.out.print(" ");
csp++;
}
} else {
int cst = 1;
while (cst <= nst) {
System.out.print("*");
cst++;
}
}
if (rows == 1 || rows == n / 2 + 1) {
int cst = 0;
while (cst <= nst) {
System.out.print("*");
cst++;
}
} else {
System.out.print("*");
}
} else {
if (rows < n) {
int csp = 0;
while (csp <= nsp) {
System.out.print(" ");
csp++;
}
} else {
int cst = 1;
while (cst <= nst) {
System.out.print("*");
cst++;
}
}
System.out.print("*");
int csp = 1;
while (csp <= nsp) {
System.out.print(" ");
csp++;
}
System.out.print("*");
}
System.out.println();
rows++;
}
}
}
*/