×

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

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.

Reserved Keywords in C++

What are reserved keywords in C++? There are a few keywords that cannot be used as identifiers as those words are reserved for some other purposes, such keywords are called reserved...

15 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

C++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

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

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

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

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.