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:

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:

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:

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:

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:

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:

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:

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.