Hello, friend good morning! 

star program code in c++


            

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.

pic

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











Post a Comment

0 Comments