×

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++.

  • Program for Printing * patterns:
    • Right Angle Triangle* pattern
#include <iostream>
using namespace std;


int main()
{
    int rows;


    cout << "Enter number of rows: ";
    cin >> rows;


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

Output:

Pattern programs in c++

Explanation:

The above code is written to print the * pattern in the right-angle triangle pattern. To print this, we have used loop statements.

  • Print the same pyramid using numbers:
#include <iostream>
using namespace std;


int main()
{
    int rows;


    cout << "Enter number of rows: ";
    cin >> rows;


    for(int i = 1; i <= rows; ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << j << " ";
        }
        cout << "\n";
    }
    return 0;
}

Output:

Pattern programs in c++

Explanation:

The above code is written to print the right-angled pyramid in the same manner as the previous one but by using numbers. The procedure followed here is the same as in the previous example, but instead of *, we are printing the number.

  • Right angle triangle patter using alphabets:
#include <iostream>
using namespace std;


int main()
{
    char input, alphabet = 'A';


    cout << "Enter the last uppercase character you want to print in the last row: ";
    cin >> input;


    for(int i = 1; i <= (input-'A'+1); ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << alphabet << " ";
        }
        ++alphabet;


        cout << endl;
    }
    return 0;
}

Output:

Pattern programs in c++

Explanation:

The above code is written to pint the right-angle triangle pattern of alphabets. Unlike in the previous example, we have taken input from the user to know to which row the output should print in char form. Using loops, we have printed the output.

  • To print the right angle triangle in the reverse format:
#include <iostream>
using namespace std;


int main()
{
    int rows;


    cout << "Enter number of rows: ";
    cin >> rows;


    for(int i = rows; i >= 1; --i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << "* ";
        }
        cout << endl;
    }


    return 0;
}

Output:

Pattern programs in c++

Explanation:

In the previous codes, we printed the right angle triangle, but here we have printed the right angle triangle in the reverse format. Using for loop, we have printed the reverse triangle format.

  • Program to print Pyramid
#include <iostream>
using namespace std;


int main()
{
    int space, rows;


    cout <<"Enter number of rows: ";
    cin >> rows;


    for(int i = 1, k = 0; i <= rows; ++i, k = 0)
    {
        for(space = 1; space <= rows-i; ++space)
        {
            cout <<"  ";
        }


        while(k != 2*i-1)
        {
            cout << "* ";
            ++k;
        }
        cout << endl;
    }
    return 0;
}

Output:

Pattern programs in c++

Explanation:

The above code is written to print the pyramid. Using the loop statements, we have printed the pyramid shape of *.

  • Program to print Pyramid in reverse format:
#include <iostream>
using namespace std;


int main()
{
    int rows;


    cout << "Enter number of rows: ";
    cin >> rows;


    for(int i = rows; i >= 1; --i)
    {
        for(int space = 0; space < rows-i; ++space)
            cout << "  ";


        for(int j = i; j <= 2*i-1; ++j)
            cout << "* ";


        for(int j = 0; j < i-1; ++j)
            cout << "* ";


        cout << endl;
    }


    return 0;
}

Output:

Pattern programs in c++

Explanation:

In the previous example, we printed a pyramid shape, but here we are printing the inverted pyramid shape. Similarly, as in the previous example, we printed the inverted triangle shape.

  • To print a pyramid using numbers:
#include <iostream>
using namespace std;


int main()
{
    int rows, count = 0, count1 = 0, k = 0;


    cout << "Enter number of rows: ";
    cin >> rows;


    for(int i = 1; i <= rows; ++i)
    {
        for(int space = 1; space <= rows-i; ++space)
        {
            cout << "  ";
            ++count;
        }


        while(k != 2*i-1)
        {
            if (count <= rows-1)
            {
                cout << i+k << " ";
                ++count;
            }
            else
            {
                ++count1;
                cout << i+k-2*count1 << " ";
            }
            ++k;
        }
        count1 = count = k = 0;


        cout << endl;
    }
    return 0;
}

Output:

Pattern programs in c++

Explanation:

The above code is written to print the pyramid of numbers. The same as the previous example, we have used for loop to print the pyramid of numbers.


Related Topics

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

3 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.