When it comes to pattern-making using loops many of us by-heart the concept and forget in a couple of days. And in the case of writing programs, we can say it is easier to write the logic in python, java, etc. but in C/C++ it is a bit tough because of the concatenation of the string and integer(numbers). And in order to learn the patterns we should know about patterns and loops.
So first of all what is exactly a pattern in programming?
In the programming world, a pattern is a reusable solution or piece of code to a common problem that generally programmers encounter. Patterns are like a template that can be applied to different situations.
Let me explain it in an easy way patterns can be used for various things, like designing how something looks on the screen, for example, a triangular pattern or a pyramid pattern(most often seen in programming languages). So using patterns can make it easier for programmers to write code that can be reused in different parts of the program or to visualize in a better way.
Note: patterns are not always the best solution to every problem, so programmers should use them carefully and only when appropriate.
Now wondering what is a loop when it comes to patterns.
In programming, a loop is a way to repeat a block of code multiple times. They are very helpful when we need to perform a block of code multiple times.
Let's take the example of printing tables, for printing a table of any number would you write the same line (a * a = a) 10 times just imagine how boring it would be so programmers came up with a solution of loops - there are several loops like for loop, while loop, do while loop and for time being we will go with for loop
#include <stdio.h>
int main()
{
int i, num;
/* Input a number to print table */
printf("Enter number to print table: ");
scanf("%d", &num);
for(i=1; i<=10; i++){
printf("%d * %d = %d", num, i, (num*i));
printf("\n");
}
return 0;
}
and output would be
We here use multiple loops or loop in a loop called the nested loop. An example of a nested loop is given below.
for( /*condition.....*/ ){
for( /*condition.....*/ ){
// body of loop
}
}
Here the first loop represents the number of rows you want, and the second loop is responsible for printing the pattern(mainly stars, hash, etc.).
Enough of learning what is a pattern, loop now let's have a look at an example of a pattern
#include <stdio.h>
int main()
{
int i, j, rows=3;
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
So I think you all got an overview of what is loop and how a pattern is made.
Note: I have hard-coded the no. of rows you can take the rows as input and print as much as you can.
Some advance problems
The above design of loop was for the left-aligned patterns, but for making centered aligned patterns(like pyramids) we have to think in a bit different way. Here we have to use a loop for maintaining spaces and for printing the pattern we use a while loop.
#include <stdio.h>
int main() {
int i, space, rows=3, k = 0;
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
It was just a pattern of stars you can also make patterns of the alphabet, numbers, etc.
Some common examples of patterns are:-
Hollow square pattern
Diamond/Hollow diamond pattern
Pascal's Triangle
Floyd's Triangle
So, I think that will be enough for getting the knowledge of patterns. Now I would suggest you guys have some hands-on practice and for reference and practice I am attaching some links from where you can have a broad idea of how patterns can be made, and practicing those problems would be incomplete so here is an online compiler, I think it is a better option for practicing according to me.
And for reference, JavaTpoint and GeeksForGeeks would be good enough and also for solving questions.
That's all, for now, let's catch up on another one till then keep learning and stay safe :)