Top 25 Pattern Programs in C++

Pattern Programs in C++

In this article, I will show you the Top 25 Pattern Programs in C++ that you must learn which will help you to understand the usage of nested loops. We will learn to create different geometrical shapes like Squares, triangles, Rhombus, etc. We have provided the code as well as output for all the patterns below.

1. Square Pattern Program in C++

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

Output:

Square Pattern in C++

2. Hollow Square Pattern Programs in C++

int main()
{
    int n;  
    cout << "Enter the number of rows" << endl;  
    cin >> n;  
    for(int i=1;i<=n;i++)  
    {  
        for(int j=1;j<=n;j++)  
        {  
            if(i==1 ||i==n||j==1||j==n)  
            {  
            cout << "*";  
            }  
            else  
            cout << " ";  
        }  
        cout << endl;
    }  
      
    return 0;  
}

Output:

Hollow Square Pattern in C++

3. Hollow Square Pattern with Diagonal in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=n; j++) {
            if(i==1 ||i==n||j==1||j==n-i+1||i==j||j==n) {
                cout << "*";
            } else {
                cout << " ";
            }
        }
        cout << endl;
    }
    return 0;
}

Output:

Hollow Square Pattern with Diagonal in C++

4. Right Angled Triangle pattern

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

Output:

Right Angled Triangle pattern

5. Hollow Right Angle Triangle pattern

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=i; j++) {
            if(j==1|| i==j || i==n ) {
                cout<< "*";
            } else
                cout<< " ";
        }
        cout << endl;
    }
    return 0;
}

Output:

Hollow Right Angle Triangle pattern

6. Mirrored Right Angle Triangle pattern

#include<iostream>
using namespace std;
int main()
{
    int i, j, k, rows;
    cout << "Enter number of rows ";
    cin >> rows;
    cout << "Star Pattern\n";
    for(i = 1; i <= rows; i++) {
        for(j = 0; j <= rows - i; j++) {
            cout << " ";
        }
        for(k = 0; k < i; k++) {
            cout << "*";
        }
        cout << "\n";
    }
    return 0;
}

Output:

Mirrored Right Angle Triangle pattern

7. Mirrored Hollow Right Angle Triangle pattern

#include <iostream>
using namespace std;
int main()
{
    int n;
    int m = 1;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout << "star pattern \n";
    for(int i=n; i>=1; i--) {
        for(int j=1; j<=i-1; j++) {
            cout << " ";
        }
        for(int k=1; k<=m; k++) {
            if(k==1 || k==m || m==n)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
        m++;
    }
    return 0;
}

Output:

Mirrored Hollow Right Angle Triangle pattern

8. Hollow Inverted Right Angle Triangle

#include <iostream>
using namespace std;
int main()
{
    int n;
    int m;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    m=n;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<i; j++) {
            cout << " ";
        }
        for(int k=1; k<=m; k++) {
            if(i==1 || k==1 || k==m)
                cout << "*";
            else
                cout << " ";
        }
        m--;
        cout << endl;
    }
    return 0;
}

Output:

Hollow Inverted Right Angle Triangle

9. Hollow Inverted Mirrored Right Angle Triangle

#include <iostream>
using namespace std;
int main()
{
    int n;
    int m = 1;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    for(int i=n; i>=1; i--) {
        for(int j=1; j<=i; j++) {
            if(j==1 || j==i || i==n)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Output:

Hollow Inverted Mirrored Right Angle Triangle

10. Inverted Mirrored Right Angle Triangle

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

Output:

Inverted Mirrored Right Angle Triangle

11. Inverted Right Angle Triangle pattern

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

Output:

Inverted Right Angle Triangle pattern

12. Pyramid Pattern Program in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    int m;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout << "Pyramid pattern is\n";
    m = n;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m-1; j++) {
           cout<< " ";
        }
        for(int k=1; k<=2*i-1; k++) {
            cout << "*";
        }
        m--;
        cout << endl;
    }
    return 0;
}

Output:

Pyramid Pattern in C++

13. Hollow Pyramid Pattern

#include <iostream>
using namespace std;
int main()
{
    int n;
    int m;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout << "Hollow Pyramid pattern is\n";
    m = n;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m-1; j++) {
            cout << " ";
        }
        for(int k=1; k<=2*i-1; k++) {
            if(k==1 || k==2*i-1 || i==n)
                cout << "*";
            else
                cout << " ";
        }
        m--;
        cout << endl;
    }
    return 0;
}

Output:

Hollow Pyramid Star Pattern

14. Inverted Pyramid Star Pattern

#include <iostream>
using namespace std;
int main()
{
    int n;
    int m;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout << "Inverted Pyramid pattern is\n";
    m = 1;
    for(int i=n; i>=1; i--) {
        for(int j=1; j<m; j++) {
            cout << " ";
        }
        for(int k=1; k<=2*i-1; k++) {
            cout << "*";
        }
        m++;
        cout << endl;
    }
    return 0;
}

Output:

Inverted Pyramid Star Pattern

15. Hollow Inverted Pyramid Pattern

#include <iostream>
using namespace std;
int main()
{
    int n;
    int m;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout << "Hollow Inverted Pyramid pattern is\n";
    m = 1;
    for(int i=n; i>=1; i--) {
        for(int j=1; j<m; j++) {
            cout << " ";
        }
        for(int k=1; k<=2*i-1; k++) {
            if(k==1 || k==2*i-1 || i==n)
                cout << "*";
            else
                cout << " ";
        }
        m++;
        cout << endl;
    }
    return 0;
}

Output:

Hollow Inverted Pyramid Pattern

16. Rhombus Pattern in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout<< "Rhombus Star Pattern is \n";
    for(int i=n; i>=1; i--) {
        for(int j=1; j<=i-1; j++) {
            cout << " ";
        }
        for(int k=1; k<=n; k++) {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Output:

Rhombus Pattern in C++

17. Hollow Rhombus Pattern Program in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout<< "Hollow Rhombus Star Pattern is \n";
    for(int i=n; i>=1; i--) {
        for(int j=1; j<=i-1; j++) {
            cout << " ";
        }
        for(int k=1; k<=n; k++) {
            if(i==1 || i==n || k==1 || k==n)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Output:

Hollow Rhombus Pattern in C++

18. Mirrored Rhombus Pattern in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout<< "Mirrored Rhombus Star Pattern \n";
    for(int i=1; i<=n; i++) {
        for(int j=1; j<i; j++) {
            cout << " ";
        }
        for(int k=1; k<=n; k++) {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Output:

Mirrored Rhombus Pattern in C++

19. Hollow Mirrored Rhombus Pattern

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout<< "Hollow Mirrored Rhombus Star Pattern \n";
    for(int i=1; i<=n; i++) {
        for(int j=1; j<i; j++) {
            cout << " ";
        }
        for(int k=1; k<=n; k++) {
            if(i==1 || i==n || k==1 || k==n)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Output:

Hollow Mirrored Rhombus Pattern

20. Diamond Pattern Program in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout << "Diamond pattern \n";
    int spaces=n-1;
    int stars=1;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=spaces; j++) {
            cout << " ";
        }
        for(int k=1; k<=stars; k++) {
            cout << "*";
        }
        if(spaces>i) {
            spaces=spaces-1;
            stars=stars+2;
        }
        if(spaces<i) {
            spaces=spaces+1;
            stars=stars-2;
        }
        cout << endl;
    }
    return 0;
}

Output:

Diamond Pattern in C++

21. Half Diamond Pattern in C++

#include <iostream>
using namespace std;
int main()
{
    int n, m;
    cout << "Enter the number of rows" << endl;
    cin >> n;
    cout << "Half Diamond pattern \n";
    m=1;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=i; j++) {
            cout << "*";
        }
        cout << endl;
    }
    for(int i=n-1; i>=1; i--) {
        for(int j=1; j<=i; j++) {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Output:

Half Diamond Pattern in C++

22. Plus Pattern in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number of rows(only odd)" << endl;
    cin >> n;
    cout << "Plus Pattern  \n";
    for(int i=1; i<=n; i++) {
        if(i==((n/2)+1)) {
            for(int j=1; j<=n; j++) {
                cout << "+";
            }
        } else {
            for(int j=1; j<=n/2; j++) {
                cout << " ";
            }
            cout << "+";
        }
        cout << endl;
    }
    return 0;
}

Output:

Plus Pattern in C++

23. Cross Pattern in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter number of rows"<< endl;
    cin >> n;
    int m = 2*n - 1;
    for ( int i = 1; i <= m; i++ ) {
        for ( int j = 1; j <= m; j++ ) {
            if (j == i || j == (m + 1 - i))
                cout << "* ";
            else
                cout << " ";
        }
        cout << endl;
    }
}

Output:

Cross Pattern in C++

24. Right Arrow Pattern Program in C++

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

Output:

Right Arrow Pattern in C++

25. Left Arrow Pattern in C++

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter number of rows(odd only)"<< endl;
    cin >> n;
    cout << "Left Arrow Star Pattern \n";
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=n-i; j++) {
            cout << " ";
        }
        for(int k=0; k<=n-i; k++) {
            cout << "*";
        }
        cout << endl;
    }
    for(int i=1; i<n; i++) {
        for(int j=1; j<i+1; j++) {
            cout << " ";
        }
        for(int k=1; k<=i+1; k++) {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Output:

Left Arrow Pattern in C++

Conclusion

This brings us to the end of the Top 25 Pattern Programs in C++. I hope this article helped you to get more clarity on the fundamentals of C++. Once you are comfortable with the logic of these patterns, you will be able to write most of the pattern printing programs in C++ on your own.

Thank you for visiting our website.


Also Read:

Share:

Author: Ayush Purawr