First of All What is Pattern ?
In programming, a pattern is basically a reusable solution to a common problem or a structured way of organizing code so itβs easier to understand, maintain, and extend.
But Pattern is Different and Types Let's Explore That ->
Square Pattern π₯
Square Pattern in programming Means Printing or Generating Output in the Shape of the Square usually using Loops.
// Square Pattern
int n = 4;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout<<j<<" ";
}
cout<<endl;
}
Triangle Pattern π©
Triangle Pattern in programming Means Printing or Generating Output in the Shape of the Triangle usually using Loops.
// Triangle Pattern
int n = 4;
for (int i = 0; i <n; i++)
{
for (int j = 0; j <i+1; j++)
{
cout<<(i+1);
}
cout<<endl;
}
Reverse Triangle Pattern π©
Reverse Triangle Pattern in programming Means Printing or Generating Output in the Shape of the Reverse Triangle usually using Loops.
// Reverse Triangle Pattern
int n = 4;
for (int i = 0; i <n; i++)
{
for (int j=i+1; j>0; j--)
{
cout<<j;
}
cout<<endl;
}
Floyd's Triangle Pattern π’
Floydβs Triangle is a right-angled triangular pattern of natural numbers, where numbers are placed consecutively starting from 1 in each row.
- The first row has 1 number.
- The second row has 2 numbers.
- The third row has 3 numbers, and so on.
// Floyd's Triangle Pattern
int n = 4;
int num = 1;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j > 0; j--)
{
cout<<num<<" ";
num++;
}
cout<<endl;
}
Inverted Triangle Pattern π©
An inverted triangle pattern is a triangle shape printed upside down.
1 - It starts with the widest row at the top (usually the maximum number of emojis or characters).
2 - Each next row below has one less emoji, shrinking the pattern until it ends with a single emoji at the bottom.
3 - This pattern helps practice nested loops and output formatting in programming.
// Inverted Triangle Pattern
int n = 4;
int num = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
cout<<" ";
}
for (int j = 0; j < n-i; j++)
{
cout<<(i+1)<<" ";
}
cout<<endl;
}
Pyramid Pattern πΊ
A pyramid pattern is a symmetrical triangle shape aligned at the center.
1 - The first row starts with one emoji at the center.
2 - Each subsequent row increases the number of emojis by two, forming a pyramid shape.
3 - The width grows until the base is reached, usually defined by the number of rows.
Pyramid patterns are great to practice nested loops and spacing in programming..
// Pyramid Pattern
int n = 4;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-i-1; j++)
{
cout<<" ";
}
for (int j = 1; j <= i+1; j++)
{
cout<<j;
}
for (int j = i; j >0; j--)
{
cout<<j;
}
cout<<endl;
}
So Hop you Enjoy This Article.
Note : My English isnβt perfect yet, but that wonβt stop me. Iβm learning every day, and I will master it soon!
Top comments (0)