Introduction to Star Patterns in C++
In this article, first, we are going to see how to use C++ programming to work with various star patterns programs with the help of examples. In any programming language, star patterns are one of the common patterns that are widely used because it helps to improve logical thinking and flow control knowledge. To create pattern in the C++ language, you just have to use two loops or three loops. The number of loops depends on the pattern that you need to create. For patter minimum two are used i.e. one for row and one for a column. The First loop is called an outer loop that shows the rows and the second loop is called an inner loop that shows columns.
Examples of patterns in C++ language
Let us discuss some examples to understand the concept of patterns in C++ easily.
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 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 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 the 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 hollow star pyramid in a diamond pattern
In the following program, the user can enter the 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 has been a guide to Star Patterns In c++. Here we have discussed the introduction and different patterns of examples in c++ along with output. You can also go through our other suggested articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses