Print patterns in C++
This tutorial will cover the following top pattern C++ programs that use stars (*), numbers, or other characters.
- Triangle pattern
- Basic pyramid pattern
- Inverted basic pyramid pattern
- Inverted triangle pattern
- Diamond pattern
- Half diamond pattern
- Flipped half-diamond pattern
- Hour glass pattern
- Number pyramid pattern
- Pyramid Pattern of Numbers without Reassigning
1. Triangle Pattern
// C++ code illustrating the star triangle pattern
#include <iostream>
using namespace std;
//Function to illustrate the printing pattern
void triangle(int x)
{
//Count of the spaces
int a, b, p = x;
// In this scenario, the outer loop
// will handle the number of rows, i.e., x.
for (a = 1; a <= x; a++) {
// inside loop related to columns
for (b = 1; b <= x; b++) {
// prerequisite for printing the star pattern
if (b >= p)
cout << "* ";
else
cout << " ";
}
p--;
cout << "\n";
}
}
int main()
{
int x = 6;
triangle(x);
return 0;
}
Output:
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp1.png)
2. Basic Pyramid Pattern
Approach 1: Printing a basic pyramid design using recursion
// C++ code that uses recursion to illustrate the star pattern
#include <iostream>
using namespace std;
// recursive method to display * on the console
void pyramid(int x)
{
if(x == 0) // base case
{
return;
}
cout << "* ";
pyramid(x - 1); // recursive calling
}
// printing pattern demonstration using a recursive function
void star(int x)
{
// base case
if (x == 0)
{
return;
}
else
{
// call recursive Function
star(x - 1);
// print "* " x times
pyramid(x);
//Line of ending after every row
cout << endl;
}
}
// Driver Function
int main()
{
int x = 6;
star(x);
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp2.png)
Approach 2: Using a while loop to print the previous pattern
// C++ code illustrating the star pattern
#include <iostream>
using namespace std;
//Function to illustrate the printing pattern
void pyramid(int x)
{
// outer loop to manage the quantity
// of rows (n in our example)
int a = 0, b = 0;
while (a < x) {
// Inner loop for managing the number of columns
// values shifting according to the outer loop
while (b <= a) {
// Printing stars
cout << "* ";
b++;
}
b = 0; // To make it start from scratch and
// print * equal to a, we must reset
// the b value.
a++;
// Ending Line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int x = 6;
pyramid(x);
return 0;
}
Output:
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp3.png)
3. Inverted basic Pyramid Pattern
Approach 1: Using a for loop, print the basic pyramid pattern 180 degrees turned
// C++ code illustrating the star pattern
#include <iostream>
using namespace std;
//Function to illustrate the printing pattern
void pyramid(int x)
{
// In this scenario, the outer loop
// will handle the number of rows x.
int a = x, b = 0;
while (a > 0) {
// inner loop to manage the number of columns
// values shifting according to the outer loop
while (b < a) {
// Stars printing
cout << "* ";
b++;
}
b = 0; // For it to print * equal to a
// and begin at the beginning, we must
// reset the value of b.
a--;
// lines end after each row
cout << endl;
}
}
// Driver Code
int main()
{
int x = 6;
pyramid(6);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp4.png)
Approach 2: Using a while loop, print the pattern mentioned previously
// C++ code that uses recursion to
//Print the inverted pyramid pattern
#include <bits/stdc++.h>
using namespace std;
// recursive printing function
void pyramid(int x)
{
if(x == 0) // base case
{
return;
}
cout << "* ";
pyramid(x - 1); // recursive calling for printing
}
void star(int x)
{
if (x == 0) {
return;
}
pyramid(x);
cout << endl;
star(x - 1);
}
// driver function
int main()
{
int x = 6;
star(6);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp5.png)
4. Inverted Triangle Pattern
Approach 1: Using a for loop, print the inverted triangle pattern
// Code in C++ to illustrate the inverted triangle pattern
#include <iostream>
using namespace std;
//Function to illustrate the printing pattern
void inverted(int x)
{
//Count of spaces in between
int p = 2 * x - 2;
// In this scenario, the outer loop
// will handle the number of rows x
for (int a = x; a > 0; a--) {
// Internal loop to manage number spaces
// values varying according to need
for (int b = 0; b < x - i; b++)
cout << " ";
// p is decreased following each cycle.
p = p - 2;
// Inner loop for managing the number of columns
// values shifting according to the outer loop
for (int b = 0; b < a; b++) {
// Stars printing
cout << "* ";
}
//Line ends after every row
cout << endl;
}
}
// Driver Code
int main()
{
int x = 6;
inverted(x);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp6.png)
Approach 2: Using a while loop, print the inverted triangle pattern
// C++ code illustrating the printing of patterns
#include <iostream>
using namespace std;
//Function to illustrate the printing pattern
void inverted(int x)
{
int a = x, b = 0, p = 0;
while (a > 0) {
// count the spaces
while (p < (x - a)) {
cout << " ";
p++;
}
// Since we wish to execute p from the
// beginning, we are resetting p
p = 0;
while (b < a) {
cout << "* ";
b++;
}
// resetting p to allow it to begin from 0.
b = 0;
a--;
cout << endl;
}
}
// Driver Code
int main()
{
int x = 6;
// Function Call
inverted(x);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp7.png)
5. Diamond Pattern
// A C++ program that prints a pattern of a diamond-shaped star (*)
#include <iostream>
using namespace std;
// Function to print 2*x-1 rows and 2*x-1 columns in a diamond pattern
void printDiamond(int x)
{
// Out of the whole 2*x-1
// rows, we first print the top x rows.
for (int a = 1; a <= x; a++) {
// To print the number of spaces in the (a)th row, b iterates till x-a
for (int b = 1; b <= x - a; b++) {
cout << " ";
}
// To print the number of 2*a-1 stars (*)
// in the (a)th row, p iterates till 2*a-1
for (int p = 1; p <= 2 * a - 1; p++) {
cout << "*";
}
//Line terminates after the (a)th row's printing of 2*a-1 stars
cout << endl;
}
// We now output the remaining (x-1) pattern rows
for (int a = 1; a <= x - 1; a++) {
// b repeats until a, at which point it
// prints the number of spaces in the (a)th row
for (int b = 1; b <= a; b++) {
cout << " ";
}
// p continues till 2*(x-a)-1 that prints 2*(x-a)-1
// number of stars (*) in the (a)th row
for (int p = 1; p <= 2 * (x - a) - 1; p++) {
cout << "*";
}
//Line terminates after the (a)th row's printing of 2*(x-a)-1 stars
cout << endl;
}
}
int main()
{
int x = 6;
// Function call
printDiamond(x);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp8.png)
6. Half Diamond Pattern
Printing half diamond pattern using loop
// A C++ program that uses a nested
// for loop to print a half-diamond
// pattern
#include <iostream>
using namespace std;
//Function to create a half-diamond pattern
void halfdiamond(int x)
{
// Out of all the 2*x-1 rows
// we first print the top x rows
for (int a = 1; a <= x; a++) {
// b loops until it reaches the number a of stars (*)
// in the (a)th row, separated by a single space
for (int b = 1; b <= a; b++) {
// printing stars one spacing apart
cout << "*" << " ";
}
// line finishes when the stars in the (a)th row are printed
cout << endl;
}
// We now print the triangle's additional (x-1) rows
for (int a = 1; a <= x - 1; a++) {
// To print the x-a number of stars (*)
// in the (a)th row, separated by a single
// space, b iterates till x-a.
for (int b = 1; b <= x - a; b++) {
// printing stars one spacing apart
cout << "*" << " ";
}
//Line stops when the (a)th row's x-a stars are printed
cout << endl;
}
}
//Driver Code
int main()
{
int x = 6;
//Function Call
halfdiamond(x);
return 0;
}
7. Flipped half-diamond pattern
Printing flipped half diamond pattern using for loop
// Printing the flipped half-diamond pattern with a C++ program
#include <iostream>
using namespace std;
// The Function to print the pattern of a flipped half-diamond
void flippedhalfdiamond(int x)
{
// Out of the whole 2*x-1 rows
//We first print the top x rows.
for (int a = 1; a <= x; a++) {
// To print the number of spaces in the (a)th row, b iterates till x-a.
for (int b = 1; b <= x - a; b++) {
cout << " ";
}
// To display the number of stars (*) in the (a)th row
// p iterates till a
for (int p = 1; p <= a; p++) {
cout << "*";
}
// line finishes when the stars in the (a)th row are printed
cout << endl;
}
// We now print the triangle's additional (x-1) rows
for (int a = 1; a <= x - 1; a++) {
// b repeats until a, at which point it
// prints the number of spaces in the (a)th row
for (int b = 1; b <= a; b++) {
cout << " ";
}
// To display the number of stars (*) in the (a)th row,
// p iterates till x-a.
for (int p = 1; p <= x - a; p++) {
cout << "*";
}
//Line stops when the (a)th row's x-a stars are printed.
cout << endl;
}
}
int main()
{
int x = 6;
// Function call
flippedhalfdiamond(x);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp9.png)
8. Hourglass pattern
// Hour Glass Pattern Printing Program in C++
#include <iostream>
using namespace std;
void hourglass(int x)
{
if (x <= 0) {
return;
}
for (int a = 0; a < 2 * x - 1; a++) {
if (a < x) {
for (int b = 0; b < 2 * x - a - 1; b++) {
if (b < a) {
cout << " ";
}
else {
cout << "* ";
}
}
}
else {
for (int b = 0; b < a + 1; b++) {
if (b < 2 * x - a - 2) {
cout << " ";
}
else {
cout << "* ";
}
}
}
cout << endl;
}
cout << endl;
}
// drive code
int main()
{
int x = 6;
hourglass(x);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp10.png)
9. Number pyramid pattern
Approach 1: Using for loop to print number pyramid pattern
// C++ code illustrating the printing of a numerical pattern
#include <iostream>
using namespace std;
// A function that shows the printing of the pattern
void numberpattern(int x)
{
// Initialising the initial value
int init = 1;
// In this scenario, the outer loop will handle the number of rows x.
for (int a = 0; a < x; a++) {
// Inner loop for managing the number of columns
// values shifting according to the outer loop
for (int b = 0; b <= a; b++)
cout << init << " ";
//Increasing the number in every column
init = init + 1;
// Last Line following every row
cout << endl;
}
}
// Driver Code
int main()
{
int x = 6;
// Function Call
numberpattern(x);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp11.png)
Approach 2: Using a while loop to print the number pyramid pattern
// C++ Code to Print Patterns
#include <iostream>
using namespace std;
//Function to illustrate the printing pattern
void numberpattern(int x)
{
int a = 1, b = 0;
while (a <= x) {
while (b <= a - 1) {
cout << a << " ";
b++;
}
b = 0;
a++;
cout << endl;
}
}
// Driver Code
int main()
{
int x = 6;
// Function Call
numberpattern(x);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp12.png)
10. Pyramid Pattern of Numbers without Reassigning
// C++ code to show how to print a number pattern
#include <iostream>
using namespace std;
//Function as an example of a printing pattern
void pyramid(int x)
{
int a = 1, b = 0;
// In this case, a val variable with the value 1 is declared.
int val = 1;
while (a <= x) {
while (b <= a - 1) {
// numbers printing
cout << val << " ";
// In this case, val is increasing with each iteration.
val++;
b++;
}
b = 0;
a++;
// Last Line following every row
cout << endl;
}
}
// Driver Code
int main()
{
int x = 6;
pyramid(x);
return 0;
}
Output
![Print patterns in C++](https://static.tutorialandexample.com/cpp/print-patterns-in-cpp13.png)
graphics.ce more for cache memory loading following bucket sorting.language..