Number Patterns 1
I hope you got enough practice on star pattern problems and must have understood the role of loops and conditionals there. Star patterns are patterns that uses only a single variable: '*'. Now let's take this pattern game to the next level: Number Patterns 😎
A number pattern refers to a sequence of numbers (multi-variable patterns) arranged in a particular order. These problems utilises a combination of control flow concepts especially for loops. Hence practicing these problems helps in a better understanding of loop concepts and how to use them effectively. It gives an extensive practice to a variety of problems, especially the complex ones!
Just remember 1 thing about pattern problems:
One row at a time
Examples
Now let's solve some problems on number patterns.
Square Number Patterns
Square number pattern refers to patterns that have equal no. of columns in each row as shown in example figure 1.0s & 1s Patterns
Let's solve problems on printing square patterns consisting of only 0s and 1s
Observe the most minor details and break down the problem based upon the same. Later just convert those observations into the code. I know it's easier said than done but you yourselves must have experienced the same even while solving non-programming pattern problems like in an aptitude test. Same thing applies here : )
Pattern 1: Print 1, 0 number pattern at alternate rows
Write a program to print 0s and 1s at alternate rows.
Input
rows = 6
cols = 5
Output
11111
00000
11111
00000
11111
00000
Intuition
To write something like this the first approach that comes to my mind is write 5 times 1s in a row and then same for the 0s. Later on just copy and paste. But, don't you think that loops are the one that does this copy pasting for us. Even in a single row, rather than writing 1 or 0 5 times, it would be better that I run a for loop from 1 to 5. Ring some bells?
This pattern requires a nested loop! But this concept is nothing new as you might already have used nested loops multiple times in previous patterns like that of star patterns. But now the question arises how to flip between 0s and 1s??
Observation: Notice that 1st, 3rd, 5th rows are printed as 1 and 2nd, 4th, and 6th rows are printed as zeroes. This indicates that for all odd rows 1 is printed and for even rows 0 is printed. Hence, before printing numbers inside inner loop, check if the row no. is even or odd. If the current row is odd then print 1 otherwise 0.
Logic
- Input number of rows and columns to print from user. Store it in variables say rows and cols.
- To iterate through rows run an outer loop from 1 to rows.
- Run another inner loop to iterate through columns from 1 to cols.
- Inside the inner loop before printing any number, we need to check condition. Which is for every odd rows 1 is printed and for every even rows 0 is printed. Hence, check if(i%2 == 1) then print 1 otherwise 0.
- Finally, after the inner loop when all columns are printed move to next line.
Solution
#include <iostream>
using namespace std;
int main() {
int rows, cols;
/* Input rows and columns from user */
cout << "Enter number of rows: "<<endl;
cin >> rows;
cout << "Enter number of columns: "<<endl;
cin >> cols;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
// Print 1 if current row is odd
if (i % 2 == 1) {
cout << "1";
} else {
cout << "0";
}
}
cout << endl;
}
return 0;
}
Pattern 2: Print 1, 0 number pattern at alternate columns
Write a program to print 0s and 1s at alternate columns.
Input
rows = 6
cols = 5
Output
01010
01010
01010
01010
01010
01010
Intuition
If we relate to the first problem, we can say that we will need two for loops. One outer loop to iterate over each row and one inner loop to iterate over each column. But now let's observe the changes that we would have to do for this pattern.
Observation: We can observe here that for the 1st, 3rd and 5th columns, 0s are printed whereas for 2nd and 4th columns 1s are printed. Hence, for the odd columns we print 0s and for the odd columns we print 1s. Relating to the first problem again, we'll apply same conditions on the columns rather than the rows. So just like we checked for rows in our last problem, we'll check for columns this time!
Logic
- Input number of rows and columns to print from user. Store it in some variable say rows and cols.
- To iterate through rows run an outer loop from 1 to rows.
- To iterate through columns run an inner loop from 1 to cols.
- Inside the inner loop print 1 if current column is even otherwise print 0. Means if(j%2==0) then print 1 otherwise print 0.
- Finally move to the next line after printing one column.
Solution
#include <iostream>
using namespace std;
int main() {
int rows, cols;
/* Input rows and columns from user */
cout << "Enter number of rows: "<<endl;
cin >> rows;
cout << "Enter number of columns: "<<endl;
cin >> cols;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
// Print 1 if current column is even
if (j % 2 == 0) {
cout << "1";
} else {
cout << "0";
}
}
cout << endl;
}
return 0;
}
Pattern 3: Print box number pattern of 1s and 0s
Input
rows = 6
cols = 5
Output
11111
10001
10001
10001
10001
11111
Intuition
Kudos for solving the above 2 problems. This problem is a little different from the previous two and is not based upon odds and evens check as we did in our first problem. Only thing that would be the same is the requirement of nested loops.
Observation: Here 1 is printed for
- Every column of first and last row.
- Start and end column of each row.
This means that we need to add if else conditions in our inner loop for checking our current rows and columns. We'll use conditions like:
if 'current_row = first_row OR current_column =first_column OR current_row = last_row OR current_column = last_column' then print 1
else print 0.
Logic
- Input number of rows and columns to print from user. Store it in variables say rows and cols.
- To iterate through rows run an outer loop from 1 to rows.
- To iterate through columns run an inner loop from 1 to cols.
- Inside the inner loop before printing 1 check the above mentioned condition. Which is if(i==1 || i==rows || j==1 || j==cols) then print 1 otherwise 0.
- Finally, move to the next line after printing all columns of a row.
Solution
#include <iostream>
using namespace std;
int main() {
int rows, cols;
/* Input rows and columns from user */
cout << "Enter number of rows: "<<endl;
cin >> rows;
cout << "Enter number of columns: "<<endl;
cin >> cols;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
/*
* Print 1 if it's the first or last row
* Print 1 if it's the first or last column
*/
if (i == 1 || i == rows || j == 1 || j == cols) {
cout << "1";
} else {
cout << "0";
}
}
cout << endl;
}
return 0;
}
Pattern 4: Print chessboard number pattern with 1 and 0
Input
rows = 5
cols = 5
Output
10101
01010
10101
01010
10101
Intuition
Try to think the above pattern as a matrix where 1 and 0 is printed at every alternate element. Hence we need to track the alternate positions and print the value accordingly. Now the question arises that how can we track this?
To keep track of alternate element, we will use an extra variable k. k will have 2 values and k's value will decide that what we should print.
Let's say k can have two possible values i.e. -1 and 1. For k = 1, we'll print 1 otherwise print 0. Hence initially k value would be 1 so we'll print 1. Since we need to print alternate elements, we'll change the value of to -1. Next time, when we check the value of k, it'll be -1 and so we'll print 0. We'll again change k's value and repeat this step after printing pattern's value everytime.
Logic
- Input number of rows and columns to print from user. Store it in some variable say rows and cols.
- Initialize a variable to keep track of alternate element, say k = 1.
- To iterate through rows run an outer loop from 1 to rows.
- To iterate through columns run an inner loop from 1 to cols.
- Inside the inner loop print 1, 0 for alternate elements. Say if(k == 1) then print 1 otherwise 0. After printing change the value of k = k * -1.
Note: Multiplying 1 with -1 gives -1 and -1 with -1 gives 1. You can remember this as a short trick for flipping values rather than applying if else condition : ) - Finally, move to next line after printing all columns of a row.
Solution
#include <iostream>
using namespace std;
int main() {
int rows, cols;
int k = 1;
/* Input rows and columns from user */
cout << "Enter number of rows: "<<endl;
cin >> rows;
cout << "Enter number of columns: "<<endl;
cin >> cols;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
if (k == 1) {
cout << "1";
} else {
cout << "0";
}
// Alternate between 1 and 0
k *= -1;
}
// Reverse the pattern if the number of columns is even
if (cols % 2 == 0) {
k *= -1;
}
cout << endl;
}
return 0;
}
Pattern 5: Print box number pattern with plus in center
Assumption: Input rows and cols can only be odd for the pattern to be printed correctly.
Input
rows = 5
cols = 5
Sample Output
11011
11011
00000
11011
11011
Intuition
Observation: 0 is printed for 3rd row and column. 3rd rows and columns are nothing but the middle columns and rows. This means that we need to print 0s in middle rows and columns else print 1.
Now how can we get this middle row or column? For odd numbers like 5, the middle value would be (number+1)/2 or (5+1)/2 = 6/2 = 3. Hence, i.e. 0 is printed for all (cols +1) / 2 and (rows+1) / 2 else 1 would be printed.
This means that inside the inner loop, a condition should be added that checks that if 'current_row = (rows+1) / 2 OR current_column = (cols +1) / 2' then print 0 else print 1.
Logic
- Input number of rows and columns to print from user. Store it in variables say rows and cols.
- To iterate through rows run an outer loop from 1 to rows.
- To iterate though columns run an inner loop from 1 to cols.
- We already know that 0 is printed only for central rows and columns otherwise 1 is printed. Hence, if(i == rows/2 || j == cols/2), then print 0 otherwise print 1.
- Finally, move to the next line after printing all columns of a row.
Solution
#include <iostream>
using namespace std;
int main() {
int rows, cols;
int centerRow, centerCol;
/* Input rows and columns from user */
cout << "Enter number of rows: "<<endl;
cin >> rows;
cout << "Enter number of columns: "<<endl;
cin >> cols;
centerRow = (rows + 1) / 2;
centerCol = (cols + 1) / 2;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
// Print 0 for central rows or columns
if (centerCol == j || centerRow == i) {
cout << "0";
} else if ((cols % 2 == 0 && centerCol + 1 == j) || (rows % 2 == 0 && centerRow + 1 == i)) {
// Print an extra 0 for even rows or columns
cout << "0";
} else {
cout << "1";
}
}
cout << endl;
}
return 0;
}
Pattern 6: Print box number pattern of 1 and 0 with cross center
Assumption: Input rows and cols can only be odd for the pattern to be printed correctly.
Input
rows = 5
cols = 5
Output
10001
01010
00100
01010
10001
Intuition
Observation: In the given pattern 1 is printed according to row numbers
For 1st row: in first and last column
For 2nd row: in second and second last column
For 3rd row: in third and third last column
For 4th row: in fourth and fourth last column
For 5th row: in fifth and fifth last column
If we generalise this pattern then 1 is printed only when:
- Current column is equal to the current row.
- Current column equals (total columns + 1) – current row.
Hence we need to apply the same condition inside our inner loop that checks the current column value. If it's equal to any of the above 2 values i.e. current row OR (total columns + 1) – current row, then we print 1 else 0.
Logic
- Input number of rows and columns to print from user. Store it in some variable say rows and cols.
- To iterate through rows run an outer loop from 1 to rows.
- To iterate through columns run an inner loop from 1 to cols.
- Inside the inner loop check if(i == j || (j == (cols+1)-i)) then print 1 otherwise print 0.
- Finally, move to the next line after printing all columns of a row.
Solution
#include <iostream>
using namespace std;
int main() {
int rows, cols;
/* Input rows and columns from user */
cout << "Enter number of rows: "<<endl;
cin >> rows;
cout << "Enter number of columns: "<<endl;
cin >> cols;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
if (i == j || (j == (cols + 1) - i)) {
cout << "1";
} else {
cout << "0";
}
}
cout << endl;
}
return 0;
}
Pattern 7: Print circle box number pattern with 1 and 0
Finally, we have reached to the last problem of this article. Tired or enthusiasm is just the same as in the first problem? Don't worry guys, this problem would be a cakewalk for you if you solved the previous ones! Let's see what's the pattern for this.
Input
rows = 5
cols = 5
Output
01110
10001
10001
10001
01110
Intuition
This problem is just a minor variation of the third pattern. If you solved that then this problem is nothing for you guys 💪
Clearly observe the given pattern. It is very similar to the third (box) pattern:
11111
10001
10001
10001
11111
Here the only difference is: 0 gets printed for cornermost elements instead of 1. What conditions does this imply in the inner loop?? Before that, remember that these conditions would be added in addition to the box loop conditions as given in problem 3 that if the current row or current column are the 1st or last columns then print 1.
Now, we'll also check that if (current_row = 1 OR last) AND (current_column = 1 OR last) then it indicates the corner of the pattern and hence print 0.
Else if (current_row = first_row) OR (current_column =first_column) OR (current_row = last_row) OR (current_column = last_column) then print 1.
This would indicate the elements on edges excluding the corner ones as we already checked that before and printed 0 for it.
else print 0.
This indicates the inner elements of the box.
Logic
- Input number of rows and columns to print from user. Store it in variables say rows and cols.
- To iterate through rows run an outer loop from 1 to rows.
- To iterate though columns run an inner loop from 1 to cols.
- Inside the inner loop, print 1 for edges except for corner elements. Which is if(i==1 || i==rows || j==1 || j==rows) and not a corner then print 1 otherwise print 0.
- Finally, move to the next line after printing all columns of a row.
Solution
#include <iostream>
using namespace std;
int main() {
int rows, cols;
/* Input rows and columns from user */
cout << "Enter rows: "<<endl;
cin >> rows;
cout << "Enter columns: "<<endl;
cin >> cols;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
// Print corner element
if ((i == 1 || i == rows) && (j == 1 || j == cols)) {
cout << "0";
} else if (i == 1 || i == rows || j == 1 || j == cols) {
// Print edge
cout << "1";
} else {
// Print center
cout << "0";
}
}
cout << endl;
}
return 0;
}
In this article, we practiced variety of problems upon square number patterns consisting of 0s and 1s. Practicing questions just after understanding the theoretical concepts can be a bit overwhelming but don't undermine the amount of confidence that it gives when you solve a problem. As already said, observation is the key for pattern problems so keep an eye to crack the pattern. Once you figure out the condition, then it's just a cakewalk.
Remember Practice leads to Perfection.
See you in the next article!