×

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets (A, B, C, D, E, F, G, H,…) to make forms like pyramids, triangles, and so on in this tutorial. The C++ pattern programs listed below, as well as instructions on how to run and print them, are covered in this tutorial.

  1. Simple Half Pyramid Pattern of Stars Using While Loop
  2. Simple Half Pyramid Pattern of Stars Using For Loop
  3. Simple Half Inverted Pyramid Pattern of Stars
  4. Simple Complete Pyramid Pattern of Stars
  5. Simple Complete Inverted Pyramid Pattern of Stars
  6. Simple Half Pyramid Pattern of Numbers Using Loop
  7. Simple Half Inverted Pyramid of Numbers
  8. Simple Complete Pyramid Pattern of Numbers
  9. Pascal’s Triangle Pyramid of Numbers
  10. Half Pyramid Pattern of Alphabets
  11.  Full Pyramid Pattern of Alphabets

Programs:

1. Print a Simple Half Pyramid Pattern of Stars Using While Loop

Code:

//CPP Code to Print Simple half Pyramid using while loop


#include <iostream>
using namespace std;


int main ()
{
	//considering a variable
	int j; 
	
	//asks user for a input
	cout << "Enter the height of pyramid: "; 
	
	//storing value in variable j
	cin >> j;
	
	//considering a variable and initialize with 1
	int row = 1;
	
	//logic
	while ( row <= j )
	{
		//taking a variable and initialize with 1
		int col = 1;
		
	//logic
	while ( col <= row )
	{
		//printing star 
		cout << "*";
		
		//incrementing col
		col = col + 1;
		}	
		cout << endl;
		
		//incrementing row
		row = row + 1;
	}
}

Output: (If User puts 7 as input)

Enter the height of pyramid: 7
*
**
***
****
*****
******
******* 

2. Print a Simple Half Pyramid Pattern of Stars Using For Loop

Code:

//CPP Code to Print Simple half Pyramid using for loop


#include <iostream>
using namespace std;


int main ()
{
	//considering a variable
	int row; 
	
	//asks user for a input
	cout << "Enter the rows of pyramid: "; 
	
	//storing value in variable row
	cin >> row;
	
	//logic
	for(int m = 1; m <= row; ++m)
    {
        for(int n = 1; n <= m; ++n)
        {
        	//printing stars
            cout << "*";
        }
        cout << "\n";
    }
    return 0;
}

Output: (If the user puts 8 as input)

Enter the rows of pyramid: 8
*
**
***
****
*****
******
*******
********

3. Print a Simple Half Inverted Pyramid of Stars

Code:

//CPP Code to Print Simple half inverted Pyramid 


#include <iostream>
using namespace std;


int main ()
{
	//considering a variable
	int row; 
	
	//asks user for a input
	cout << "Enter the rows of pyramid: "; 
	
	//storing value in variable row
	cin >> row;
	
	//logic
	for(int m = 0; m <= row; m++)
    {
        for(int n = m; n <=  row; n++)
        {
        	//printing stars
            cout << "*";
        }
        cout << "\n";
    }
    return 0;
}

Output: (If the user puts 6 as input)

Enter the rows of pyramid: 6
*******
******
*****
****
***
**
*

4. Print Simple Complete Pattern Pyramid of Stars

Code:

//CPP Code to Print Simple complete inverted Pyramid 
#include<iostream>
using namespace std;


int main()
{
		//considering variables
    int row, m, p, n;
    
    //asks user for a input
    cout << "Enter the number of rows: ";
    
    //storing value in variable row
    cin >> row;
    
    //logic
    for(m=1; m<=row; m++)
    {
        for( p= row; p > m; p-- )
            cout<<" ";
        for( n = 0; n < m; n++)
        
        //printing stars
            cout<<"* ";
        cout << endl;
    }
    cout << endl;
    return 0;
}

Output: (If the user puts 5 as input)

Enter the number of rows: 5
    *
   * *
  * * *
 * * * *
* * * * *

5. Print Simple Complete Inverted Pattern Pyramid of Stars

Code:

//CPP Code to Print Simple complete inverted Pyramid 
#include<iostream>
using namespace std;


int main()
{
		//considering variables
    int row, m, p, n;
    
    //asks user for a input
    cout << "Enter the number of rows: ";
    
    //storing value in variable row
    cin >> row;
    
    //logic
    for(m=1; m<=row; m++)
    {
        for( p= 1; p < m; p++ )
            cout<<" ";
        for( n = m; n <= row; n++)
        
        //printing stars
            cout<<"* ";
        cout << endl;
    }
    cout << endl;
    return 0;
}

Output: (If user puts 11 as input)

Enter the number of rows: 11
* * * * * * * * * * *
 * * * * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * * * * * *
     * * * * * *
      * * * * *
       * * * *
        * * *
         * *
          *

6. Simple Half Pyramid Pattern of Numbers Using Loop

Type 1:

//CPP Code to Print Simple half Pyramid using while loop


#include <iostream>
using namespace std;


int main ()
{
	//considering a variable
	int j; 
	
	//asks user for a input
	cout << "Enter the height of pyramid: "; 
	
	//storing value in variable j
	cin >> j;
	
	//considering a variable and initialize with 1
	int row = 1;
	
	//logic
	while ( row <= j )
	{
		//taking a variable and initialize with 1
		int col = 1;
		
	//logic
	while ( col <= row )
	{
		//printing star 
		cout << row;
		
		//incrementing col
		col = col + 1;
		}	
		cout << endl;
		
		//incrementing row
		row = row + 1;
	}
}

Output: (If the user puts 5 as input)

Enter the height of pyramid: 5
1
22
333
4444
55555

Type 2:

//CPP Code to Print Simple half Pyramid using for loop
#include <iostream>
using namespace std;


int main()
{
	//considering variable r
    int r; 
    
    //asks user for a input
    cout << "Enter the number of rows: ";
    
    //storing value in r
    cin >> r;


		//logic
    for(int m = 1; m <= r; ++m)
    {
        for(int n = 1; n <= m; ++n)
        {
            cout << n << " ";
        }
        cout << "\n";
    }
    return 0;
}

Output: (If the user puts 6 as input)

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

Type 3:

//CPP Code to Print Simple half Pyramid using while loop


#include <iostream>
using namespace std;


int main()
{
	 //taking variable h
	int h;
	
	//asks user for input
	cout << " Enter the height of pyramid : ";
	
	//storing input
	cin >> h;
	
	int row = 1;
	
	//logic
	while ( row <= h)
	{
		int col = 1;
		int val = row ;
		
		while ( col <= row )
		{
			cout << val;
			val ++;
			col ++;
		}
		cout << endl;
		row ++;
	 } 
}

Output: (If the user puts 4 as input)

Enter the height of pyramid : 4
1
23
345
4567

Type 4:

//CPP Code to Print Simple half Pyramid using while loop


#include <iostream>
using namespace std;


int main()
{
	 //taking variable h
	int h;
	
	//asks user for input
	cout << " Enter the height of pyramid : ";
	
	//storing input
	cin >> h;
	
	int m = 1;
	
	//logic
	while ( m <= h)
	{
		int n = 1;
		
		while ( n <= m )
		{
			cout << ( m - n + 1 ) <<" ";
			n ++;
		}
		cout << endl;
	 m ++;
	 } 
}

Output: (If user puts 5 as input)

Enter the height of pyramid : 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

Type 5:

//CPP program to print half pyramid pattern


#include <iostream>
using namespace std;


int main()
{
	//considering variables
    int h, m, n, no = 1;


  //asks user for a input
    cout << "Enter the height of pyramid: ";
    
    //storing value
    cin >> h;




		//logic
    for(m = 1; m <= h; m++)
    {
        for(n = 1; n <= m; ++n)
        {
            cout << no << " ";
            ++no;
        }


        cout << endl;
    }


    return 0;
}

Output: (If user puts 5 as input)

Enter the height of pyramid: 5
1
2 3
4 5 6
7 8 9 10
11 2 13 14 15

7. Simple Half Inverted Pyramid Pattern of Numbers Using Loop

Code:

//CPP Code to Print Simple half inverted Pyramid using for loop


#include <iostream>
using namespace std;


int main()
{
	//taking variable as h
    int h;


    //put a value
    cout << "Enter height of pyramid: ";
    
    //storing value
    cin >> h;


	//logic
    for(int m = h; m >= 1; --m)
    {
        for(int n = 1; n <= m; ++n)
        {
            cout << n << " ";
        }
        cout << endl;
    }


    return 0;
}

Output: (If user puts 5 as input)

Enter height of pyramid: 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

8. Simple Complete Pyramid Pattern of Numbers

Type 1:

//CPP Code to Print Simple complete Pyramid


#include <iostream>
using namespace std;




int main()
{
//taking variables
    int rs, c = 0, c1 = 0, m = 0;


//asks user for a input
    cout << "Enter number of rows: ";


//storing value
    cin >> rs;


//logic
    for(int i = 1; i <= rs; ++i)
    {
        for(int sp = 1; sp <= rs-i; ++sp)
        {
            cout << "  ";
            ++ c;
        }


        while(m != 2 * i - 1)
        {
            if (c <= rs - 1)
            {
                cout << i + m << " ";
                ++ c;
            }
            else
            {
                ++ c1;
                cout << i + m - 2 * c1 << " ";
            }
            ++m ;
        }
        c1 = c = m = 0;


        cout << endl;
    }
    return 0;
}

Output: (If the user puts 6 as input)

Enter number of rows: 6
          1
        2 3 2
      3 4 5 4 3
    4 5 6 7 6 5 4
  5 6 7 8 9 8 7 6 5
6 7 8 9 10 11 10 9 8 7 6

Type 2:       

//CPP Code to Print Simple complete Pyramid


#include<iostream>
using namespace std;


int main()
{
//considering variable
	int rs;


//asks user for a input,
	cout << "Enter the height of Pyramid:  ";


//storing value in variable rs
	cin >> rs;


//logic
	for (int m = 1; m <= rs; m++)
	{
		for (int n = rs; n > m; n--)
		{
			cout << " ";
		}
		for (int s = 1; s <= m; s++)
		{
			cout << m << " ";
		}
		cout << "\n";
	}
}

Output: (If user puts 5 as input)

Enter the height of Pyramid:  5
    1
   2 2
  3 3 3
 4 4 4 4
5 5 5 5 5

9. Pascal’s Triangle Pyramid of Numbers

Code:

#include <iostream>
using namespace std;


int main()
{
//considering variables
    int h, pt = 1;


//asks user for a input
    cout << "Enter number of rows: ";


//storing value in variable h
    cin >> h;


//logic
    for(int m = 0; m < h; m++)
    {
        for(int sp = 1; sp <= h-m; sp++)
            cout <<"  ";


        for(int n = 0; n <= m; n++)
        {
            if (n == 0 || m == 0)
                pt = 1;
            else
                pt = pt * (m-n+1)/n;


            cout << pt << "   ";
        }
        cout << endl;
    }


    return 0;
}

Output: (If the user puts 6 as input)

Enter number of rows: 6
            1
          1   1
        1   2   1
      1   3   3   1
    1   4   6   4   1
  1   5   10   10   5   1

10. Half Pyramid Pattern of Alphabets

Code:

//cpp to print half pyramid pattern of alphabets


#include <iostream>
using namespace std;


int main()
{
//considering variables
    char ip, alph = 'A';


//asks user for a input
    cout << "Enter any uppercase character to print in the end row: ";
   //storing input
 cin >> ip;


//logic
    for(int m = 1; m <= (ip-'A'+1); ++m)
    {
        for(int n = 1; n <= m; ++n)
        {
            cout << alph << " ";
        }
        ++alph;


        cout << endl;
    }
    return 0;
}

Output: (If user puts “F” as input)

Enter any uppercase character to print in the end row: F
A
B B
C C C
D D D D
E E E E E
F F F F F F

11. Full Pyramid Pattern of Alphabets

Code:

//cpp to print full pyramid pattern of alphabets


#include <iostream>  
using namespace std; 
 
int main()  
{  
//taking variable
 char alpha= 'A';    
    int sp, m, n, o, p;   


//asks user for input
     cout << "Enter the height of Pyramid: ";


//storing value
	 cin >> sp;
	 
//logic
    for( m = 1; m <= sp; m++ )    
    {    
        for( n = sp; n >= m; n-- )    
            cout<<" ";    
        for(o = 1; o <= m; o++)    
            cout << alpha++;    
            alpha--;    
        for( p = 1; p < m; p++)    
            cout<<--alpha;    
        cout<<"\n";    
        alpha='A';    
    }    
return 0;  
}  

Output: (If user puts 5 as input)

Enter the height of Pyramid: 5
     A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA

Related Topics

Data Hiding in C++

C++ : High-performance apps can be made using the cross-platform language C++. Bjarne Stroustrup created C++ as an addition to the C language. Programmers have extensive control over memory and system...

3 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays...

3 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

C++ | C Plus Plus While loop

In this article, we will discuss the C++ while loop with its syntax, use, key features, key points, pseudo code, and examples. What is the While Loop? The “while loop” is a...

4 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.