DEV Community

Cover image for Write Awesome Pattern in Programming 🟦🟨πŸŸ₯
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on

Write Awesome Pattern in Programming 🟦🟨πŸŸ₯

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;
        }
Enter fullscreen mode Exit fullscreen mode

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;
        }
Enter fullscreen mode Exit fullscreen mode

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;
       }
Enter fullscreen mode Exit fullscreen mode

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;
     }
Enter fullscreen mode Exit fullscreen mode

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;
     }
Enter fullscreen mode Exit fullscreen mode

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;
   }
Enter fullscreen mode Exit fullscreen mode

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)