Star pattern in C++ using For Loops
Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.
In the C++ programming language, you need two or three loops to generate a pattern. The number of loops you require depends on the pattern you want to make. A minimum of two patterns are used, first for each row and the second for each column. The first loop, known as an outer loop which display rows, while the second loop, known as an inner loop which display columns.
What is For Loop?
A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times.
The syntax of For Loop
In C++, a for loop is written as
for ( initialize; condition; incrementation )
{
statement(x);
}
The programmer or user inserts the number of rows according to how many stars he wants to print.
1st pattern: Program in C++ to print half star pyramid pattern:
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cout << "Enter number of rows: ";
cin >> n;
for(i = 1; i <= n; i++) // outer loop(to print rows)
{
for(j = 1; j <= i; j++) //inner loop(to print columns)
{
cout << "* ";
}
//Ending line after each row //
cout << "\n";
}
return 0;
}
Output:
Enter number of rows: 4
*
* *
* * *
* * * *
2nd pattern: Program in C++ to print inverted half star pyramid pattern
#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:
* * * *
* * *
* *
*
3rd pattern: Program in C++ to print star pyramid pattern:
#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
for(s = i; s < n; s++)
{
cout << " ";
}
//for loop to display star equal to the row number
for(j = 1; j <= (2 * i - 1); j++)
{
cout << "*";
}
// ending line after each row
cout << "\n";
}
}
Output:
Enter the number of rows: 4
*
* * *
* * * * *
* * * * * * *
4th pattern: Program in C++ to print inverted star pyramid pattern:
#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:
Enter number of rows: 4
* * * *
* * *
* *
*
5th pattern: Program to print full star diamond pattern in C++:
#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
//loop to print the upper diamond pattern //
for(i = 0; i <= n; i++)
{
for(s = n; s > i; s--)
cout << " ";
for(j=0; j<i; j++)
cout << "* ";
cout << "\n";
}
// for loop to print the inverted diamond pattern //
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:
Enter number of rows: 7
*
* *
* * *
* * * *
* * *
* *
*