Pattern Programs in C++: Zero to Hero
If you’ve taken a basic programming course in college, you must’ve come across pattern programs. Be it C++, Java or Python, pattern programs have become a staple in programming curriculums. When I first came across pattern programs, I absolutely HATED them. To me, they seemed unnecessary and unconquerable. As I’ve delved deeper into Data Structures and Algorithms, I’ve realized the importance of pattern programs.
Pattern programs are an excellent way to gain command over loops (and nested loops) and build up logical thinking. The mental muscles which you’ll pull when trying to solve pattern programs as a beginner will prove to be extremely useful when you come across more complex algorithms.
Pattern programs are a bit of logic, a bit of math and also some intuition. We’ll solve 22 pattern programs, starting from some basic ones and moving on to some more challenging patterns. I’ll be using C++ here, but you can easily implement it in a programming language of your choice, you just have to grasp the logic.
I have used Striver’s video on pattern programs as a reference. I recommend checking out his video on the same topic if you are more of a visual learner.
So before starting, I’ll state the 4 rules which will help you solve ANY pattern program:
- Focus on the number of rows, and identify the pattern.
- Focus on the number of columns, and identify the pattern.
- Print the pattern (columnwise).
- Observe symmetry (used only in a few patterns).
Let’s get started!
Pattern 1:
- This is a very simple and straight-forward pattern program, you have n rows and n columns.
- You have an outer loop that runs n times and an inner loop that runs n times.
- Inside the inner loop, you print * and once you step out of the inner loop, you go to the next line (endl in C++).
void pattern1(int n) {
// Step 1: Focus on the number of rows (outer loop): n rows
for (int i = 0; i < n; i++) {
// Step 2: Focus on the number of columns (inner loop): n columns
for (int j = 0; j < n; j++) {
// Step 3: Printing ' * ' in the inner loop
cout<<"* ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 2:
- This one’s also fairly simple, a level above the first pattern.
- The outer loop runs n times and the inner loop runs i times.
- * is printed i times in the inner loop, and as the code exits the inner loop, we go to the next line.
void pattern2(int n) {
// Step 1: Focus on the number of rows (outer loop): n rows
for (int i = 0; i < n; i++) {
// Step 2: Focus on the number of columns (inner loop): i columns
for (int j = 0; j <= i; j++) {
// Step 3: Printing ' * ' in the inner loop 'i' number of times
cout<<"* ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 3:
- This is similar to the last pattern, instead we print numbers here.
- Follow the same logic as the last pattern, just print j in the inner loop.
void pattern3(int n) {
// Step 1: Focus on the number of rows (outer loop): n rows
for (int i = 1; i <= n; i++) {
// Step 2: Focus on the number of columns (inner loop): i columns
for (int j = 1; j <= i; j++) {
// Step 3: Printing j in the inner loop till j is less than or equals to i
cout<<j<<" ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 4:
- Here, follow the logic of pattern 3, just print i instead of j.
void pattern4(int n) {
// Step 1: Focus on the number of rows (outer loop): n rows
for (int i = 1; i <= n; i++) {
// Step 2: Focus on the number of columns (inner loop): i columns
for (int j = 1; j <= i; j++) {
// Step 3: Printing i in the inner loop till j is less than or equals to i
cout<<i<<" ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 5:
- This one’s a bit different.
- Here, in the inner loop, we keep the condition j < n-i and print *.
- E.g. when i = 0, j loop will run n times; when i = 1, j loop will run n-1 times, and so on….
- So in each iteration, the number of times the j loop will run decreases, creating the pattern above.
void pattern5(int n) {
// Step 1: Focus on the number of rows (outer loop): n rows
for (int i = 0; i < n; i++) {
// Step 2: Focus on the number of columns (inner loop): i columns ( columns decreasing in number, so n-i)
for (int j = 0; j < n-i; j++) {
// Step 3: Printing i in the inner loop till j is less than or equals to i
cout<<"* ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 6:
- This one’s similar to Pattern 5, here, we just print the value of j instead of * in the inner loop.
void pattern6(int n) {
// Step 1: Focus on the number of rows (outer loop): n rows
for (int i = 1; i <= n; i++) {
// Step 2: Focus on the number of columns (inner loop): i columns ( columns decreasing in number, so n-i)
for (int j = 1; j <= n-i+1; j++) {
// Step 3: Printing i in the inner loop till j is less than or equals to i
cout<<j<<" ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 7:
- Now this one’s slightly tricky. Here, we have to take care of 2 characters: space and *.
- We will need to divide step 2 and 3 into 2 parts: spaces and *.
- For the outer loop, the condition remains the same: i < n, as there are n rows.
- For the spaces before the stars, we can come up with the formula n — i -1, considering you initialize j and i from 0.
- For spaces, e.g. when i = 0, n-1 spaces will be printed; when i = 1, n-2 spaces will we printed, and so on…
- Now, we need to print the *. There is 1 star in the 1st row, 3 stars in the 2nd row, 5 stars in the 3rd row, etc. So we can come up with the formula: 2*i + 1.
- For stars, e.g. when i = 0, 1 * will be printed; when i = 1, 3 * will be printed and so on…
void pattern7(int n) {
// Step 1: Number of rows: n
for (int i = 0; i < n; i++) {
// Step 2.1: Spaces - Number of columns: n - i - 1, e.g. if n = 4, step 1: 3, step 2: 2, step 3: 1, step 4: 0
for (int j = 0; j < n-i-1; j++) {
// Step 3.1: Printing spaces n-i-1 times
cout<<" ";
}
// Step 2.2: Stars - Number of columns: 2 * i + 1, e.g. step 1: 1, step 2: 3, step 3: 5, step 4: 7
for (int j = 0; j < 2*i+1 ; j++) {
// Step 3.1: Printing stars 2*i+1 times
cout<<"* ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 8:
- This one’s exactly the opposite of the previous pattern, so with a bit of playing around with the conditional formulae, we can find the solution to this pattern too!
- I highly recommend you to try figuring this one on your own.
- Well this pattern also has i rows, so the outer loop’s condition will be i < n, simple as before.
- Observe the pattern and notice how the number of spaces only increases by 1 each time, so we can simply use the formula j < i (like we did in pattern 2–4), to print the spaces in incremental order.
- So, when i = 0, no spaces will be printed, when i = 1, 1 space will be printed and so on…
- The conditional formula for printing the stars can be derived as 2*n — (2*i + 1) after doing a bit of pen-paper math.
- So, when i = 0, 2*n -1 stars will be printed; when i = 1, 2*n -3 stars will be printed and so on…
void pattern8(int n) {
// Step 1: Number of rows: n
for (int i = 0; i < n; i++) {
// Step 2.1: Spaces - Number of columns: i
for (int j = 0; j < i; j++) {
// Step 3.1: Printing spaces i times
cout<<" ";
}
// Step 2.2: Stars - Number of columns: 2*n - (2*i + 1) + 1, e.g. step 1: 9, step 2: 7, step 3: 5, step 4: 3
for (int j = 0; j < 2*n - (2*i + 1) ; j++) {
// Step 3.1: Printing stars 2*i+1 times
cout<<"* ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 9:
- Observe this one carefully, it’s just a mixture of patterns 7 and 8.
- So we can come up with a cheeky solution: just execute the previous 2 patterns and we are done:
void pattern9(int n) {
pattern7(n);
pattern8(n);
}
- Sometimes simple tricks like these will do the job for you!
- But if you want to make the pattern from scratch, then the step 4: Observing Symmetry will come in handy.
- Observing symmetry, you can divide the pattern into 2 parts: upper pyramid and lower pyramid.
- For the upper pyramid, you can rewrite the code we wrote for pattern 7, and for the lower pyramid, you can rewrite the code we wrote for pattern 8.
- Observing symmetry is about breaking the pattern into parts similar to each other, and solving the problem part-by-part.
- Here’s the full code:
void pattern9_alt(int n) {
// Upper Pyramid
// Step 1.1: Number of rows: n
for (int i = 0; i < n; i++) {
// Step 2.1: Spaces - Number of columns: n - i - 1, e.g. if n = 4, step 1: 3, step 2: 2, step 3: 1, step 4: 0
for (int j = 0; j < n-i-1; j++) {
// Step 3.1: Printing spaces n-i-1 times
cout<<" ";
}
// Step 2.2: Stars - Number of columns: 2 * i + 1, e.g. step 1: 1, step 2: 3, step 3: 5, step 4: 7
for (int j = 0; j < 2*i+1 ; j++) {
// Step 3.1: Printing stars 2*i+1 times
cout<<"* ";
}
// Step 2.3: Spaces - Number of columns: n - i - 1, e.g. if n = 4, step 1: 3, step 2: 2, step 3: 1, step 4: 0
for (int j = 0; j < n-i-1; j++) {
// Step 3.3: Printing spaces n-i-1 times
cout<<" ";
}
cout<<endl;
// Step 4: Observing Symmetry: We need to make a similar pattern for the lower pyramid
}
// Lower Pyramid
// Step 1.2: Number of rows: n
for (int i = 0; i < n; i++) {
// Step 2.4: Spaces - Number of columns: i
for (int j = 0; j < i; j++) {
// Step 3.1: Printing spaces i times
cout<<" ";
}
// Step 2.5: Stars - Number of columns: 2*n - (2*i + 1) + 1, e.g. step 1: 9, step 2: 7, step 3: 5, step 4: 3
for (int j = 0; j < 2*n - (2*i + 1) ; j++) {
// Step 3.1: Printing stars 2*i+1 times
cout<<"* ";
}
// Step 2.6: Spaces - Number of columns: i
for (int j = 0; j < i; j++) {
// Step 3.3: Printing spaces i times
cout<<" ";
}
cout<<endl;
// Step 4.2: Observing Symmetry: NOT REQUIRED
}
}
Pattern 10:
- Again, this one involves symmetry! You can divide the pattern into upper and lower pyramid.
- The upper pyramid is similar to pattern 2 and the lower pyramid is similar to pattern 5.
- We can’t directly print the 2 patterns as then, the row with the maximum number of * will be printed twice. Try it and see if you can tweak it to avoid rewriting the entire code. I’ll leave this quick/shortcut solution for you to figure out.
- Observe the number of rows, we can come up with the simple formula i ≤ (2*n)-1, for the number of rows.
- As the number of stars increases till the halfway point and then decreases, the number of * will depend on whether we are before the halfway point or after it.
- So we initialize a new variable stars, and assign it the value of i if i < n, meaning i * will be printed in the upper pyramid.
- Otherwise (else), we assign it the value (2*n)-i (for the lower pyramid).
- Then, we simply run the j loop stars times and print * inside the loop.
void pattern10(int n) {
// Step 1: Focus on the number of rows (outer loop): 2*n-1 rows, step 1: 1, step 2: 3, step 3: 5, step 4: 7
for (int i = 1; i <= 2*n-1; i++) {
int stars;
// Step 2: Focus on the number of columns (inner loop): increases till halfway point, then decreases.
// Step 2.1: If i is less than or equals to n, then number of columns: i (For Upper Triangle)
if (i <= n) {
stars=i;
}
// Step 2.2: If i is greater than n, then number of columns: 2*n - i (For Lower Triangle)
else {
stars=2*n-i;
}
for (int j = 1; j <= stars; j++) {
// Step 3: Printing * in the inner loop till j is less than or equals to stars
cout<<"* ";
}
cout<<endl;
// Step 4: Observing Symmetry: Already done
}
}
Pattern 11:
- This one may seem slightly daunting, but give it a bit of time and think about it and I am sure you’ll have a rough idea on how to tackle this one.
- It is similar to the easier problems we solved earlier, we just need to apply a bit of math magic to flip 1 and 0 each time!
void pattern11(int n) {
// Step 1: Focus on the number of rows (outer loop): n rows
int start;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) start = 1; // When i is even, e.g. row 0, 2, 4
else start = 0; // When i is odd, e.g. row 1, 3, 5
// Step 2: Focus on the number of columns (inner loop): i columns ( columns increasing in number, so j<i)
for (int j = 0; j <= i; j++) {
// Step 3: Printing start in the inner loop till j is less than or equals to i
cout<<start<<" ";
// Flipping start from 0 to 1 and vice versa
start = 1 - start;
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
Pattern 12:
- Let’s take this problem step by step, and you’ll soon realize this is way simpler than you intially realized.
- The outer loop is simple, there are n rows.
- For the inner loop, we divide the problem into 3 parts: the numbers, the spaces and the numbers again.
- If you just observe the LHS of the pattern, you’ll realize it’s just pattern 3, so we can apply the same logic for the LHS.
- Now figuring out the number of spaces. Spaces decrease in each iteration, again a bit of pen-paper math will give us the formula j ≤ 2 * (n-i), note that we are initializing i and j from 1 in this problem.
- Now dealing with the numbers again, the numbers are being printed in reverse order, let’s think a bit creatively to figure this one out (taking some inspiration from the pattern 3 again).
- We can assign j the value of i and reduce the value of j in each iteration, we can achieve the RHS pattern by printing j. We can run this code till j doesn’t exceed 1. Don’t get confused, read the code for this part a couple of times and do a dry run (of this and the previous part) if you are still confused!
void pattern12(int n) {
// Step 1: Focus on the number of rows: n rows
for (int i = 1; i <= n; i++) {
// Step 2.1: Focus on the number of numbers: i numbers in ascending order, e.g. from 1 to 4
for (int j = 1; j <= i; j++) {
// Step 3.1: Printing j in the inner loop till j is less than or equals to i
cout<<j;
}
// Step 2.2: There are always 2 * (n-1) spaces, e.g. when n=4, step 1: 6, step 2: 4, step 3: 2, step 4: 0
for (int j = 1; j <= 2 * (n-i); j++) {
// Step 3.2: Printing spaces in the inner loop till j is less than or equals to 2 * (n-1)
cout<<" ";
}
// Step 2.3: Focus on the number of numbers: i numbers in descending order, e.g. from 4 to 1
for (int j = i; j >= 1; j--) {
// Step 3.1: Printing j in the inner loop till j is greater than or equals to i
cout<<j;
}
cout<<endl;
}
}
Pattern 13:
- This one’s simpler than some of our previous problems. It is very similar to pattern 3 with just a bit of twist.
- Notice how the more problems you solve, the more you understand the relationships in the pattern you are seeing for the first time.
- I recommend trying to solve this entirely on your own.
void pattern13(int n) {
// Step 1: Focus on the number of rows: n rows
int count=1;
for (int i = 1; i <= n; i++) {
// Step 2: Focus on the number of columns: i number columns
for (int j = 1; j <= i; j++, count++) {
// Step 3: Printing count in the inner loop i times
cout<<count<<" ";
}
cout<<endl;
}
}
Pattern 14:
- Before solving this one, I recommend reading a bit about ASCII table.
- In ASCII, A + 1 is B, A + 2 is C and so on…
- Use this logic to solve the above pattern.
- NOTE: A common error a lot of beginners make is initalize characters in double quotations. In C++, use single quotation for characters and double quotation for strings.
void pattern14(int n) {
// Step 1: Focus on the number of rows: n rows
int count=1;
for (int i = 0; i < n; i++) {
// Step 2: Focus on the number of columns: i columns
// E.g. when i = 3, we have to print A B C, ch <= "A" + i is same as ch <= "A" + 2 which is same as ch <= "C"
for (char ch = 'A'; ch <= 'A' + i; ch++) {
// Step 3: Printing ch in the inner loop i times
cout<<ch<<" ";
}
cout<<endl;
}
}
Pattern 15:
- Refer to the previous pattern and pattern 5 to try and create this pattern.
- For the inner loop, making some modifications to the pattern 5 should give you the desired result.
void pattern15(int n) {
// Step 1: Focus on the number of rows: n rows
int count=1;
for (int i = 1; i <= n; i++) {
// Step 2: Focus on the number of columns: i columns in decreasing order
for (char ch = 'A'; ch <= 'A' + (n-i); ch++) {
// Step 3: Printing ch in the inner loop i times
cout<<ch<<" ";
}
cout<<endl;
}
}
Pattern 16:
- This one’s pattern 4 in character form. Easy!
- Use your knowledge on ASCII and refer to the 4th pattern to get through this one quickly.
void pattern16(int n) {
// Step 1: Focus on the number of rows: n rows
int count=1;
for (int i = 0; i < n; i++) {
// Step 2: Focus on the number of columns: i columns in increasing orderf
for (char ch = 'A' + i, j = 0; j <= i; j++) {
// Step 3: Printing ch in the inner loop i times
cout<<ch<<" ";
}
cout<<endl;
}
}
Pattern 17:
- This one’s slightly challenging but symmetry will help us make this problem easier.
- Observe symmetry: how the characters increase till midpoint and then reduce till the end.
- We can devise a formula to increase the character if we haven’t crossed the midpoint, and decrease it after crossing the midpoint for each row.
- So, if j < ((2*i)+1)/2), we increase the character, else we decrease it.
- Now that we know when to increase and decrease the character, the problem is a lot easier, we’ve solved the tricky part.
- Take a look at pattern 7 and use the above formula to devise a pattern for this one!
void pattern17(int n) {
// Step 1: Number of rows: n
for (int i = 0; i < n; i++) {
// Step 2.1: Spaces - Number of columns: n - i - 1, e.g. if n = 4, step 1: 3, step 2: 2, step 3: 1, step 4: 0
for (int j = 0; j < n-i-1; j++) {
// Step 3.1: Printing spaces n-i-1 times
cout<<" ";
}
char ch='A';
// Step 2.2: Stars - Number of columns: 2 * i + 1, e.g. step 1: 1, step 2: 3, step 3: 5, step 4: 7
for (int j = 0; j < 2*i+1 ; j++) {
// Step 3.2: Printing stars 2*i+1 times
cout<<ch<<" ";
// Step 4: Observing Symmetry: char will increase till midpoint, after which it will decrease.
if (j < (2*i+1)/2) ch++;
else ch--;
}
cout<<endl;
}
}
Pattern 18:
- Here, if there are 5 rows, then the 1st row contains the 5th character, 2nd row contains 4th and 5th character.
- Here, in the inner loop, we initialize j as ‘A’+n-i, and while printing we print the value before the character (so character-1)/
- Once you figure this out, the rest of the problem is straightforward.
void pattern18(int n) {
// Step 1: Focus on the number of rows: n rows
int count=1;
for (int i = 0; i < n; i++) {
// Step 2: Focus on the number of columns: i columns in increasing order
for (char ch = 'A' + n - i; ch <= 'A'+n; ch++) { // E.g. when n=3, step 1: E, step 2: D E, step 3: C D E
// Step 3: Printing ch in the inner loop i times
char ch2 = ch - 1;
cout<<ch2<<" ";
}
cout<<endl;
}
}
Pattern 19:
- Now this looks tough right? Observe symmetry (the upper and lower part are opposite of each other), devise some formulae to achieve the star and space patterns from your experience of solving 18 such problems and you are done!
- For the upper part, break this further to LHS, the spaces and the RHS. Observe how LHS is just pattern 5 followed by i spaces, and the RHS again is just pattern 5.
- So the upper part is just patter 5 — i spaces — pattern 5
- Similarly, break down the the lower part into LHS, spaces and RHS.
- The LHS and RHS is just like pattern 2, we just have to figure out how to get the right number of spaces.
- We initalize j as 1 for the spaces and keep the conditional as j < (2*n — 2*i), this simple formula will do the trick!
void pattern19(int n) {
// Step 1.1: UPPER PART- Focus on the number of rows (outer loop): n rows
for (int i = 0; i < n; i++) {
// Step 2.1: Stars- Focus on the number of columns: n-i stars ( columns decreasing in number, so n-i)
for (int j = 0; j < n-i; j++) {
// Step 3.1: Printing * in the inner loop till j is less than n-i
cout<<"* ";
}
// Step 2.2 Spaces- Focus on the number of columns: 2*i spaces
for (int j = 0; j < 2*i; j++) {
// Step 3.2: Printing spaces in the inner loop till j is less than 2*i
cout<<" ";
}
// Step 2.3: Stars- Focus on the number of columns: n-i stars ( columns decreasing in number, so n-i)
for (int j = 0; j < n-i; j++) {
// Step 3.3: Printing * in the inner loop till j is less than n-i
cout<<"* ";
}
cout<<endl;
}
// Step 4: Observing Symmetry: We create a similar but opposite pattern for the lower part
// Step 1.2: LOWER PART- Focus on the number of rows (outer loop): n rows
for (int i = 0; i < n; i++) {
// Step 2.4: Stars- Focus on the number of columns: i+1 stars ( columns increasing in number, so i+1)
for (int j = 0; j <= i; j++) {
// Step 3.4: Printing * in the inner loop till j is less than or equals to i
cout<<"* ";
}
// Step 2.5: Spaces- Focus on the number of columns: 2*n - 2*i - 1 spaces
for (int j = 1; j < (2*n - 2*i) - 1; j++) {
// Step 3.5: Printing * in the inner loop till j is less than 2*n - 2*i - 1
cout<<" ";
}
// Step 2.6: Stars- Focus on the number of columns: i+1 stars ( columns increasing in number, so i+1)
for (int j = 0; j <= i; j++) {
// Step 3.6: Printing * in the inner loop till j is less than or equals to i
cout<<"* ";
}
cout<<endl;
}
}
Pattern 20:
- Now that you’ve solved the previous one, this should seem less daunting.
- As we did in the previous question, observe symmetry and break the upper and lower part of the pattern into LHS, spaces and RHS.
- Try solving this on your own, I’ll give you some hints.
- The upper part of this pattern bears resemblance to the lower part of the previous pattern.
- The lower part of this pattern is similar to the upper part of the previous pattern.
- 1 important distance is that in the previous question, the row with most number of stars was printed twice, while here it is just printed twice. This will affect the formula for 1 side of the pattern.
void pattern20(int n) {
// Step 1.1: Focus on the number of rows: n rows
for (int i = 0; i < n; i++) {
// Step 2.1: Stars- Focus on the number of columns: i+1 stars in increasing order
for (int j = 0; j <= i; j++) {
// Step 3.1: Printing stars in the inner loop till j <= i
cout<<"* ";
}
// Step 2.2: Spaces- Focus on the number of columns: 2*n - 2*i spaces
for (int j = 1; j < (2*n - 2*i) - 1; j++) {
// Step 2.2: Printing spaces in the inner loop till j < 2*n - 2*i - 1
cout<<" ";
}
// Step 2.3: Stars- Focus on the number of columns: i+1 stars in increasing order
for (int j = 0; j <= i; j++) {
// Step 3.3: Printing stars in the inner loop till j <= i
cout<<"* ";
}
cout<<endl;
}
// Step 4: Observing Symmetry: We create a similar but opposite pattern for the lower part
// Step 1.2: Focus on the number of rows: n rows
for (int i = 0; i < n; i++) {
// Step 2.4: Stars- Focus on the number of columns: 2*n - 2*i stars
for (int j = 0; j < n - i - 1; j++) {
// Step 3.4: Printing * in the inner loop till j < 2*n - 2*i
cout<<"* ";
}
// Step 2.5: Spaces- Focus on the number of columns
for (int j = 0; j < (2*i) + 2; j++) {
// Step 3.5: Prining spaces in the inner loop till j is less than or equals to 2*n - 2*i
cout<<" ";
}
// Step 2.6: Stars- Focus on the number of columns: 2*n - 2*i stars
for (int j = 0; j < n - i - 1; j++) {
// Step 3.6: Printing * in the inner loop till j < 2*n - 2*i
cout<<"* ";
}
cout<<endl;
}
}
Pattern 21:
- This one’s a bit simpler than the previous 2 patterns.
- We create the upper border by printing n stars.
- For n-2 times, we print a * followed by n-2 spaces, followed by another *.
- We create the lower border by printing n stars again.
void pattern21(int n) {
// Upper Border - n stars
for (int j = 0; j < n; j++) {
// Printing * n times
cout<<"* ";
}
cout<<endl;
// Step 1: Focus on the number of rows: n rows
for (int i = 0; i < n-2; i++) {
// Step 2.1: Main Pattern - Printing 1 * followed by n - 2 spaces followed by another *
cout<<"* ";
for (int j = 0; j < n-2; j++) {
// Step 3.1: Printing spaces n-2 times
cout<<" ";
}
cout<<"* ";
cout<<endl;
}
// Lower Border - n stars
for (int j = 0; j < n; j++) {
// Printing * n times
cout<<"* ";
}
}
Pattern 22:
- Now onto the last problem!
- Relax, this one’s just to show how we can create complicated patterns using the 4 fundamental rules we’ve learned.
- It’s totally okay if you don’t get it in your first try ( or the first few tries). This one may take a bit of time to completely absorb. It took me quite a bit of time to get this one!
- We calculate the distance of each output position from the top, bottom, left and right, and print n — the minimum distance from the 4 positions (top, bottom, left, right). This gives us the sequence of numbers that we need to create this pattern.
void pattern22(int n) {
// Step 1: Focus on the number of rows: 2*n - 1 rows
for (int i = 0; i < 2*n - 1; i++) {
// Step 2: Focus on the number of columns: 2*n - 1 columns
for (int j = 0; j < 2*n - 1; j++) {
int top = i;
int bottom = j;
int right = (2*n - 2) - j;
int left = (2*n - 2) - i;
// Step 3: Printing the minimum distance of 4 directions after subtracting it from n
cout<<(n - min(min(top, bottom), min(left, right)))<<" "; // As min takes only 2 args at a time
}
cout<<endl;
}
}
And with that, we are done with 22 pattern programs! We started off with some really simple ones and climbed the ladder to create some impressive complex patterns.
I am sure you are more confident not only about pattern programs but also loops (especially nested loops) and logic-based questions. Strengthening your base will really pay off when you finally encounter data structures.
If you want to give some feedback, brainstorm an interesting project, or just drop a Hi, contact me at vanshkharidia7@gmail.com.
Please do read some of my other blogs if you found this one interesting! Have a great day :)