C++ program to print a right-angled number Pyramid

C++ program to print a right-angled number Pyramid:

In this post, we will learn how to print a right-angled number Pyramid in C++. The program will take the height of the Pyramid as an input from the user and it will print that Pyramid.

The Pyramid can be printed by using increasing numbers like 1, 2, 3,… etc. or it can be printed by using a single number like 1 or 0. I will show you both ways in this post. If you can write a program to print this Pyramid by using any number, you can easily modify it to use any other number or character.

Before we start writing the program, let’s try to understand how the algorithm works.

Algorithm to print a right-angled Pyramid:

Let’s take a look at the below Pyramid:

0 
0 0 
0 0 0 
0 0 0 0 
0 0 0 0 0

We are using 0 to print this pyramid. If you look closely,

  • The height of this pyramid is 5.
  • For the row 1, it prints 1 character.
  • For the row 2, it prints 2 characters etc.
  • So, if we consider that the row starts from 1 and ends at the height value, we have to print row number of characters on each row.

We can use two loops to write this program.

  • The outer loop will point to each row of the Pyramid. It will run from 1 to the height of the Pyramid.
  • The inner loop will print the characters on each row. It will run for current row number of times. For example, if the current row is 3, it will run for 3 times, if the current row is 4, it will run for 4 times etc.
  • The inner loop will print the characters.

So, the below algorithm we can use to write this program:

  • Take the height value as an input from the user.
  • Run one for loop from 1 to height.

    • Run one inner for loop to print the characters. Run it for the current value of the outer for loop.
    • Print the value or the number on each iteration.
  • Print a newline at the end of each iteration of the outer for loop. This will move the cursor to the next line.

C++ program to print a right-angled Pyramid:

Let’s write down the C++ prgram to print the Pyramid with 1. You can change the program to print the Pyramid with any other number or character if you want.

#include <iostream>
using namespace std;

int main()
{

	int i, j, rows;

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

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

	return 0;
}

Here,

  • We have declared three integer variables, i, j and rows. i and j are used in the loops and rows holds the total rows count of the Pyramid.
  • It asks the user to enter the rows number. It reads that value and stores it in the rows variable.
  • The outer for loop runs for rows number of time. This loop is used to point to each row of the Pyramid.
  • The inner for loop runs for i number of times. This inner loop is used to print the content of the rows, for this example we are printing 1, but we can print any other character as well.
  • Once the outer loop is completed, print a new line. So, it will start the printing for the next row.

If you run this program, it will print output as like below:

Enter total number of rows: 5
1 
1 1 
1 1 1 
1 1 1 1 
1 1 1 1 1 

Enter total number of rows: 10
1 
1 1 
1 1 1 
1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 1 
1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 

C++ program to print a number right-angled Pyramid:

Let’s modify the above program to print a right-angled Pyramid using increasing numbers. For example, let’s try to print a right-angled Pyramid similar to below:

#include <iostream>
using namespace std;

int main()
{

	int i, j, rows, number = 1;

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

	for (i = 1; i <= rows; i++)
	{
		for (j = 1; j <= i; j++)
		{
			cout << number
				 << " ";
			number++;
		}
		cout << endl;
	}

	return 0;
}

Here, we have initialized an integer value number as 1 and we are printing its value inside the inner loop. Also, we are incrementing the value of number by 1 after it is printed.

If you run this program, it will work for small number of rows:

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

But it will not align if the number of rows is larger than 4.

Enter total number of rows: 5
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

Because, we are printing only a single space after each digit in the rows.

To avoid this, we need to set the total width of each output as like below:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

	int i, j, rows, number = 1;

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

	for (i = 1; i <= rows; i++)
	{
		for (j = 1; j <= i; j++)
		{
			cout << setw(4) << number;
			number++;
		}
		cout << endl;
	}

	return 0;
}

setw sets the total width of the cout result. So, it will always set it as 4. If we print a single digit, it will add three blank spaces before the digit. If we print a double digit number, it will add two blank spaces before that number. So, the numbers will be aligned vertically.

If you run this program, it will print output as like below:

Enter total number of rows: 10
   1
   2   3
   4   5   6
   7   8   9  10
  11  12  13  14  15
  16  17  18  19  20  21
  22  23  24  25  26  27  28
  29  30  31  32  33  34  35  36
  37  38  39  40  41  42  43  44  45
  46  47  48  49  50  51  52  53  54  55

  Enter total number of rows: 5
   1
   2   3
   4   5   6
   7   8   9  10
  11  12  13  14  15

You might also like: