Updated April 11, 2023
Introduction to Star Patterns in C++
The article discusses examples of using C++ programming to create various programs with star patterns. In any programming language, star patterns are widely popular because it helps improve logical thinking and flow control knowledge. Further, to create a pattern in the C++ language, one has to use two to three loops, with their numbers depending on the required pattern. At a minimum, two loops are used, one for rows and one for columns. The first loop is called an outer loop, which shows the rows, and the second loop is called an inner loop, which shows the columns.
Examples of patterns in C++ language
Let’s discuss some easy examples to understand the concept of patterns in C++.
Example 1 – Program in C++ to print half star pyramid pattern
In the following C++ program, the user can enter a number of rows to print the half-star pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cout << "Enter number of rows: ";
cin >> n;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
cout << "* ";
}
//Ending line after each row
cout << "\n";
}
return 0;
}
Output:
Example 2- Program in C++ to print an inverted half-star pyramid pattern
In the following C++ program, the user can enter the number of rows to print the inverted half-star pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cout << "Enter number of rows: ";
cin >> n;
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << "* ";
}
// ending line after each row
cout << "\n";
}
return 0;
}
Output:
Example 3- Program in C++ to print star pyramid pattern
In the following program, the user can enter the number of rows to print the star pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
for(i = 1; i <= n; i++)
{
//for loop for displaying space
or(s = i; s < n; s++)
{
cout << " ";
}
//for loop to display star equal to row number
for(j = 1; j <= (2 * i - 1); j++)
{
cout << "*";
}
// ending line after each row
cout << "\n";
}
}
Output:
Example 4- Program in C++ to enter a number of rows to print the star pyramid pattern
In the following program, the user can enter a number of rows to print the star pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
for(i = n; i >= 1; i--)
{
//for loop to put space
for(s = i; s < n; s++)
cout << " ";
//for loop for displaying star
for(j = 1; j <= (2 * i - 1); j++)
cout << "* ";
// ending line after each row
cout << "\n";
}
return 0;
}
Output:
Example 5– Program in C++ to print an inverted star pyramid pattern
In the following program, the user can enter a number of rows to print the inverted star pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
for(i = n; i >= 1; i--)
{
//for loop to put space
for(s = i; s < n; s++)
cout << " ";
//for loop for displaying star
for(j = 1; j <= i; j++)
cout << "* ";
// ending line after each row
cout << "\n";
}
return 0;
}
Output:
Example 6 – Program in C++ to enter the number of rows to print the star pyramid pattern
In the following program, the user can enter the number of rows to print the star pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
for(i = 1; i <= n; i++)
{
//for loop to put space
for(s = i; s < n; s++)
cout << " ";
//for loop for displaying star
for(j = 1; j <= i; j++)
cout << "* ";
// ending line after each row
cout << "\n";
}
return 0;
}
Output:
Example 7 – Program to print full star diamond pattern in C++
In the following program, the user can enter the number of rows for the diamond dimension to print the diamond pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
for(i = 0; i <= n; i++)
{
for(s = n; s > i; s--)
cout << " ";
for(j=0; j<i; j++)
cout << "* ";
cout << "\n";
}
for(i = 1; i < n; i++)
{
for(s = 0; s < i; s++)
cout << " ";
for(j = n; j > i; j--)
cout << "* ";
// ending line after each row
cout << "\n";
}
return 0;
}
Output:
Example 8
Code:
#include<iostream>
using namespace std;
int main()
{
int n, i , j;
cout << "Enter number of rows: ";
cin >> n;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
cout << "*";
}
cout<<"\n";
}
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << "*" ;
}
// ending line after each row
cout<<"\n";
}
return 0;
}
Output:
Example 9
Code:
#include<iostream>
using namespace std;
int main()
{
int n, i, j;
cout << "Enter number of rows: ";
cin >> n;
for(i = 1; i <= n; i++)
{
for(j = i; j < n; j++)
{
cout << " ";
}
for(j = 1; j <= i; j++)
{
cout << "*";
}
cout << "\n";
}
for(i = n; i >= 1; i--)
{
for(j = i; j <= n; j++)
{
cout << " ";
}
for(j = 1; j < i; j++)
{
cout<<"*";
}
// ending line after each row
cout<<"\n";
}
return 0;
}
Output:
Example 10-Program to print hollow star pyramid
In the following program, the user can enter a number of rows to print the hollow star pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<iostream>
using namespace std;
int main()
{
int r, i, j, s;
cout << "Enter number of rows: ";
cin >> r;
for(i = 1; i <= r; i++)
{
//for loop to put space in pyramid
for (s = i; s < r; s++)
cout << " ";
//for loop to print star
for(j = 1; j <= (2 * r - 1); j++)
{
if(i == r || j == 1 || j == 2*i - 1)
cout << "*";
else
cout << " ";
}
//ending line after each row
cout << "\n";
}
return 0;
}
Output
Example 11-Program to print inverted hollow star pyramid pattern
In the following program, the user can enter a number of rows to print the inverted hollow star pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<iostream>
using namespace std;
int main()
{
int r, i, j, s;
cout << "Enter number of rows: ";
cin >> r;
for(i = r; i >= 1; i--)
{
//for loop to put space in pyramid
for (s = i; s < r; s++)
cout << " ";
//for loop to print star in pyramid
for(j = 1; j <= 2 * i - 1; j++)
{
if(i == r || j == 1 || j == 2*i - 1)
cout << "*";
else
cout << " ";
}
cout << "\n";
return 0;
}
Output
Example 12- Program to print a hollow star pyramid in a diamond pattern
In the following program, the user can enter a number of rows to print the hollow star pyramid in a diamond pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<iostream>
using namespace std;
int main()
{
cout << "Enter size of Daimond: ";
int n, i, j, m = 1, k;
cin >> n;
for(i = 0; i <= n; i++)
{
for(j = n; j > i; j--)
{
cout << " ";
}
cout << "*";
if (i > 0)
{
for(k = 1; k <= m; k++)
{
cout << " ";
}
m += 2;
cout << "*";
}
cout << endl;
}
m -= 4;
for(i = 0; i <= n-1; i++)
{
for(j = 0; j <= i; j++)
{
cout << " ";
}
cout << "*";
for(k = 1; k <= m; k++)
{
cout << " ";
}
m -= 2;
if(i != n-1)
{
cout << "*";
}
//ending line after each row
cout << endl;
}
return 0;
}
Output:
Recommended Articles
This is our guide to Star Patterns in C++. Here are some further related articles for expanding understanding: