Drawing Pattern in C++

Drawing Pattern in C++ using Nested Loops:

In this article, I am going to discuss how to draw Patterns using Nested Loops in C++ Language with examples. Please read our previous articles, where we discussed Nested Loops in C++ with examples. Here, we will learn how to draw patterns, different patterns we can draw easily using nested ‘for’ loop.

Drawing Pattern in C++

We will display the following pattern:

Drawing Pattern in C++ using Nested Loops

It is nothing but a 2D array. So, we can write it as,

how to draw Patterns using Nested Loops in C++ Language with examples

Now let us look at the program of this pattern.

Program to Print Pattern 1:
#include <iostream>
using namespace std;
int main()
{
    int n, count = 0;
    cout << "Enter Number: ";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            count++;
            cout << count << " ";
        }
        cout << endl;
    }
}
Output:

how to draw Patterns using Nested Loops in C++ Language with examples

Drawing Pattern 2:

Now, we will display the following pattern:

how to draw Patterns using Nested Loops in C++ Language how to draw Patterns using Nested Loops in C++ Language

Let us write the program for,

how to draw Patterns using Nested Loops in C++

Program to Print Pattern 2a:
#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter Number: ";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            cout << "* ";
        }
        cout << endl;
    }
}
Output:

how to draw Patterns using Nested Loops in C++

Now let us write the program for,

how to draw Patterns using Nested Loops in C++

Program to Print Pattern 2b:
#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter Number: ";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
           if (i > j)
           {
              cout << "  ";
           }
           else
           {
              cout << "* ";
           }
        }
        cout << endl;
    }
}
Output:

how to draw Patterns using Nested Loops

Drawing Pattern 3:

Now we will display the following pattern:

Drawing Pattern in C++ using Nested Loops Drawing Pattern in C++ using Nested Loops

Let us write the program for,

Drawing Pattern in C++ using Nested Loops

Program to Print Pattern 3a:
#include <iostream>
using namespace std;
int main()
{
    int n, count = 0;
    cout <<"Enter Number: ";
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
           if(i + j >= n - 1)
              cout << "* ";
           else
              cout << "  ";
        }   
        cout << endl;
    }
}
Output:

Drawing Pattern in C++ using Nested Loops

Now let us write the program for,

Drawing Pattern in C++ Language

Program to Print Pattern 3b:
#include <iostream>
using namespace std;
int main()
{
    int n, count = 0;
    cout << "Enter Number: ";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (i + j <= n - 1)
              cout << "* ";
            else
              cout << "  ";
        }
        cout << endl;
    }
}
Output:

Drawing Pattern in C++ Language

In the next article, I am going to discuss Multidimensional Array in C++ with examples. Here, in this article, I try to explain how to draw patterns using Nested Loops in C++ with examples. I hope you enjoy this Drawing Pattern in C++ Language with examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *