Pattern Programming Using C++ Programming Language

Introduction to Pattern Programming Using C++ Programming Language.

It takes a lot of nested loops to print patterns using a programming language. In this article, I will walk you through pattern programming using the C ++ programming language.

If you can print patterns using a programming language then you can understand that you can design algorithms by framing any idea which is an indispensable quality of a coder. Here you will learn how to print patterns using the C ++ programming language.

Also, Read – The Best Programming Language for Competitive Coding.

Pattern Programming using C ++

Now I will walk you through how to print patterns using the C ++ programming language by showing you various examples of pattern programming. All the models explained below are classified according to their difficulty.

Rectangle Pattern:

Rectangle pattern

The first example of pattern programming with C ++ is a rectangle pattern. A rectangle is a 2D shape in geometry, having 4 sides and 4 corners. Now let’s see how to print rectangle patterns using C ++:

#include<iostream>
using namespace std;
int main(){
    int row, col;
    cin>>row>>col;
    for (int i = 1; i<=row; i++){
        for (int j = 1; j<=col; j++){
            cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

Hollow Rectangle Pattern:

hollow rectanlge

The second example of pattern programming with C ++ is a hollow rectangle. A hollow rectangle pattern is the same as above but it will be hollow from the inside. Let’s see how to implement it using C ++:

#include<iostream>
using namespace std;
int main(){
    int row, col;
    cin>>row>>col;
    for (int i = 1; i<=row; i++){
        for (int j = 1; j <= col; j++){
            if (i == 1 || i == row || j == 1 || j == col){
                cout<<"*";
            }
            else{
                cout<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}

Inverted Half Pyramid:

inverted half triangle

The third example of pattern programming is an inverted half pyramid that I will implement using numbers. Let’s see how to implement an inverted half pyramid:

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    for (int i = n; i>=1; i--){
        for(int j=1; j<=i; j++){
            cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

Floyd’s Triangle:

Floyd's triangle pattern

Floyd’s Triangle is a rectangle triangular array of numbers, commonly used in print designs. It is defined by filling the lines of the triangle with consecutive numbers, starting with a 1 in the upper left corner. Let’s see how to implement Floyd’s triangular pattern using C ++:

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    int count = 1;
    for (int i = 1; i<=n; i++){
        for(int j = 1; j<=i; j++){
            cout<<count<<" ";
            count++;
        }
        cout<<endl;
    }
    return 0;

}

Butterfly Pattern:

butterfly pattern

Butterfly Pattern is one of the most complex patterns that we can implement using any programming language. The hardest part is the above part of the butterfly pattern, so when coding these patterns, divide them into two halves and then start framing solutions for it.

Now let’s see how to implement a butterfly pattern using C ++:

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    for (int i = 1; i<=n; i++){
        for (int j=1;j<=i;j++){
            cout<<"*";
        }
        int space = 2*n - 2*i;
        for (int j = 1; j<=space; j++){
            cout<<" ";
        }
        for(int j = 1; j<=i;j++){
            cout<<"*";
        }
        cout<<endl;
    }
    for(int i = n; i>=1; i--){
        for (int j=1; j<=i; j++){
            cout<<"*";
        }
        int space = 2*n - 2*i;
        for (int j = 1; j<=space; j++){
            cout<<" ";

        }
        for (int j = 1; j <= i; j++){
            cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

So here is how to implement pattern programming using the C ++ programming language. Please feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply