C++ Program to Print Right Triangle, Half Pyramid Pattern

Right Triangle, Pyramid Pattern

C++ programs to print the half pyramid pattern have been given here. Both the right half and left half of the pyramid using *, numbers and letters have been covered.






1. Program & output to print the right half pyramid pattern




1.1. C++ Program & output to print the right half pyramid pattern using *

Code has been copied
/*****************************
  	alphabetacoder.com
C++ program to print the right
half pyramid pattern using *
******************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print *
        for (j = 1; j <= i; j++) {
            cout << "*";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 6


*

**

***

****

*****

******




1.2. C++ Program & output to print the right half pyramid pattern using numbers

Code has been copied
/*********************************
  	alphabetacoder.com
C++ program to print the right
half pyramid pattern using numbers
**********************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print numbers
        for (j = 1; j <= i; j++) {
            cout << j << " ";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 6


1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6




1.3. C++ Program & output to print the right half pyramid pattern using alphabets

Code has been copied
/***********************************
       alphabetacoder.com
C++ program to print the right
half pyramid pattern using alphabets
************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print letters
        for (j = 1; j <= i; j++) {
            cout << (char)(j + 64) << " ";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 6


A

A B

A B C

A B C D

A B C D E

A B C D E F




2. Program & output to print the left half pyramid pattern




2.1. C++ Program & output to print the left half pyramid pattern using *

Code has been copied
/********************************
    alphabetacoder.com
C++ program to print the left half
of the pyramid pattern using *
*********************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print space
        for (j = 1; j <= row - i; j++) {
            cout << " ";
        }
        // print *
        for (j = 1; j <= i; j++) {
            cout << "*";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 6


     *

    **

   ***

  ****

 *****

******




2.2. C++ Program & output to print the left half pyramid pattern using numbers

Code has been copied
/***********************************
    	alphabetacoder.com
C++ program to print the left half
of the pyramid pattern using numbers
************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print space
        for (j = 1; j <= 2 * (row - i); j++) {
            cout << " ";
        }
        // print numbers
        for (j = 1; j <= i; j++) {
            cout << j << " ";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 5


        1

      1 2

    1 2 3

  1 2 3 4

1 2 3 4 5




2.3. C++ Program & output to print the left half pyramid pattern using alphabets

Code has been copied
/*************************************
    	alphabetacoder.com
C++ program to print the left half
of the pyramid pattern using alphabets
**************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print space
        for (j = 1; j <= 2 * (row - i); j++) {
            cout << " ";
        }
        // print letters
        for (j = 1; j <= i; j++) {
            cout << (char)(j + 64) << " ";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 5


        A

      A B

    A B C

  A B C D

A B C D E




3. Program & output to print the right half pyramid pattern




3.1. C++ Program & output to print the inverted half pyramid pattern using *

Code has been copied
/*********************************
	alphabetacoder.com
C++ program to print the inverted
half pyramid pattern using *
**********************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print *
        for (j = row; j >= i; j--) {
            cout << "*";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 6


******

*****

****

***

**

*




3.2. C++ Program & output to print the inverted half pyramid pattern using numbers

Code has been copied
/*********************************
	alphabetacoder.com
C++ program to print the inverted
half pyramid pattern using numbers
**********************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print numbers
        for (j = row; j >= i; j--) {
            cout << (row - j + 1) << " ";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 6


1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1




3.3. C++ Program & output to print the inverted half pyramid pattern using alphabets

Code has been copied
/***********************************
  	alphabetacoder.com
C++ program to print the inverted
half pyramid pattern using alphabets
************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print letters
        for (j = row; j >= i; j--) {
            cout << (char)(row - j + 65) << " ";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 6


A B C D E F

A B C D E

A B C D

A B C

A B

A