×

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

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

For Loop Examples in C++

For Loop A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax of for loop In C++, a...

6 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Decimal to Hexadecimal in C++

We need to write a program in C++ that converts a decimal number into an equal hexadecimal number given a decimal value as input i.e. convert a number having a...

2 minutes read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 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.

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.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.