Patterns in C++ are the basic programs that are used for the basic understanding of any language. Two or three flow control loops are used to implement these programs.
Normally, in pattern programs minimum of two loops are used i.e.
- one loop to create row.
- another loop to create a column.
In this article we are going to see some most common patterns. Everyone should practice:
Pattern 1:
Input: An integer will be given and you are required to print the following pattern.
5
*
* *
* * *
* * * *
* * * * *
Guys I'll suggest you to first try to code it by yourself and then go through the solution
code:
#include <iostream>
using namespace std;
int main()
{
int n;
std::cin >> n; // input integer will be stored
for(int i = 1 ; i <= n ; i++) // this loop is for number of rows
{
for(int j = 1 ; j <= i ; j++) // this loop is for printing the stars in a particular row
cout <<"*"<<" ";
cout <<endl;
}
return 0;
}