Introduction to Star Patterns in C++
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;
}
xample 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:
xample 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:
xample 6✌-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
0 Comments