Updated July 4, 2023
Introduction to Patterns in C++
While learning any programming language, students must practice printing various patterns to understand and implement logical thinking and flow control knowledge of the language in a practical and more fun way. Patterns in C++ are the basic programs that are used for the basic understanding of any language. Two or three flow control loops are used to implement these programs. Normally, in pattern programs, a minimum of two loops are used, i.e., one loop to create a row and another to create a column. The First loop, which is the outer loop, represents the number of rows, and the second loop is an inner loop which means the number of columns. The basic structure of the Pattern in C++ language is given as follows:
Header: #include <iostream>
Main(): int main()
{
Variable Declaration: int z = 10;
Body: cout << z;
Return: return 0;
}
Examples of Patterns in C++
There are various patterns in C ++ language, like Star Patterns, Number Patterns, and Character Patterns. In this section, we are going to discuss how to create different patterns with the help of examples.
1. Number Patterns
This section will show how to print different number patterns in the C language.
Example #1 – Print Number Pyramid
In the following program, the user can provide the number of rows to print the number pyramid pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter the number of rows to show number pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = 1; y <= n; y++)
{
if(y <= x)
cout << y;
else
cout << " ";
}
for(y = n; y >= 1; y--)
{
if(y <= x)
cout << y;
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output:
Example #2 – Print Number Diamond
In the following program, the user can provide the number of rows to print the diamond pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter the number of rows to show number paatern: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = x; y <n; y++)
{
cout << " ";
}
for(k = 1; k < (x*2); k++)
{
cout << k;
}
cout << "\n";
}
for(x = 4; x >= 1; x--)
{
for(y = n; y > x; y--)
{
cout << " ";
}
for(k = 1; k < (x*2); k++)
{
cout << k;
}
cout << "\n";
}
return 0;
}
Output:
Example #3 – Print Number Pyramid Pattern
In the following C++ program, the user can provide the number of rows to print the number pyramid pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int x, s, n, y = 0, cntr = 0, cntr1 = 0;
cout << "Enter the number of rows to show number pattern: ";
cin >> n;
for(x = 1; x <= n; ++x)
{
for(s = 1; s <= n-x; ++s)
{
cout << " ";
++cntr;
}
while(y != 2 * x - 1)
{
if (cntr <= n - 1)
{
cout << x + y;
++cntr;
}
else
{
++cntr1;
cout << (x + y - 2 * cntr1);
}
++y;
}
cntr1 = cntr = y = 0;
cout << "\n";
}
return 0;
}
Output:
Example #4 – Print Vertical Number Pyramid
In the following C++ program, the user can provide the number of rows to print the vertical number pyramid pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter the number of rows to show number pattern: ";
cin >> n;
for(int x = 1; x < n; x++)
{
for(int y = 1; y <= x; y++)
cout << y;
cout << "\n";
}
for(int x = n; x >= 0; x--)
{
for(int y = 1; y <= x; y++)
cout << y;
cout << "\n";
return 0;
}
}
Output:
Example #5 – Print Number Pyramid
In the following C++ program, the user can provide the number of rows to print the number pyramid pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter the number of rows to show number patterns: ";
cin >> n;
for (int x = n; x >= 0; x--)
{
for (int y = 1; y <= x; y++)
cout << y;
cout << "\n";
}
for(int x = 1; x <= n; x++)
{
for(int y = 1; y <= x; y++)
cout << y;
printf("\n");
}
return 0;
}
Output:
2. Star Patterns
In this section, we will see how to print different Star patterns.
Example #1 -Print Star Diamond
In the following program, the user can provide the number of rows to print the star diamond pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, s, x, y;
cout << "Enter number of rows to show star pattern: ";
cin >> n;
for(x = 0; x <= n; x++)
{
for(s = n; s > x; s--)
cout << " ";
for(y = 0; y < x; y++)
cout << "* ";
cout << "\n";
}
for(x = 1; x < n; x++)
{
for(s = 0; s < x; s++)
cout << " ";
for(y = n; y > x; y--)
cout << "* ";
cout << "\n";
}
return 0;
}
Output:
Example #2 – Print Vertical Curve
In the following C++ program, the user can provide the number of rows to print the vertical curve pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x , y;
cout << "Enter number of rows to show star pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = 1; y <= x; y++)
{
cout << "*";
}
cout << "\n";
}
for(x = n; x >= 1; x--)
{
for(y = 1; y <= x; y++)
{
cout << "*";
}
cout << "\n";
}
return 0;
}
Output:
Example #3 – Print Hollow Number Diamond
In the following program, the user can provide the number of rows to print the hollow diamond pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the number of rows to show the star pattern: ";
int n, x, y, s = 1, k;
cin >> n;
for(x = 0; x <= n; x++)
{
for(y = n; y > x; y--)
{
cout << " ";
}
cout << "*";
if (x > 0)
{
for(k = 1; k <= s; k++)
{
cout << " ";
}
s += 2;
cout << "*";
}
cout << "\n";
}
s -= 4;
for(x = 0; x <= n -1; x++)
{
for(y = 0; y <= x; y++)
{
cout << " ";
}
cout << "*";
for(k = 1; k <= s; k++)
{
cout << " ";
}
s -= 2;
if(x != n -1)
{
cout << "*";
}
//ending line after each row
cout << "\n";
}
return 0;
}
Output:
Example #4 – Print Hollow Star Triangle
In the following program, the user can provide the number of rows to print the hollow star triangle pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y, s;
cout << "Enter number of rows to show the star pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
{
//for loop to put space in pyramid
for (s = x; s < n; s++)
cout << " ";
//for loop to print star
for(y = 1; y <= (2 * n - 1); y++)
{
if(x == n || y == 1 || y == 2 * x - 1)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output:
Example #5 – Print Star Triangle
In the following program, the user can provide the number of rows to print the star triangle pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, s, x, y;
cout << "Enter number of rows to show star pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(s = x; s < n; s++)
cout << " ";
//for loop for displaying star
for(y = 1; y <= x; y++)
cout << "* ";
cout << "\n";
}
return 0;
}
Output:
3. Character Patterns
In this section, we will see how to print different character patterns.
Example #1 – Print Consecutive Characters’ Triangle
In the following program, the user can provide the number of rows to print the consecutive characters’ triangle Triangle pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter number of rows to show character pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = 1; y <= x; y++)
{
cout << char('A' + y -1);
}
cout << "\n";
}
return 0;
}
Output:
Example #2 – Print Characters’ Triangle
In the following program, the user can provide the number of rows to print the characters’ triangle pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter number of rows to show character pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = 1; y <= x; y++)
{
cout << char('A'-1 + x);
}
printf("\n");
}
return 0;
}
Output:
Example #3 – Print Characters’ Inverted Triangle
In the following program, the user can provide the number of rows to print the characters’ inverted triangle pattern as he wants, and the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter number of rows to show character pattern: ";
cin >> n;
for(x= 1; x <= n; x++)
{
for(y = n; y >= x; y--)
{
cout << char('A'-1 + x);
}
cout << "\n";
}
return 0;
}
Output:
Conclusion
This article has seen different Numbers, stars, and Character Patterns. I hope you will find this article helpful.
Recommended Articles
This has been a guide to Patterns in C++. Here we discuss Introduction to Patterns in C++ and its Different Patterns, Examples, and Syntax. You can also go through our other suggested articles to learn more –