Coding Pyramid Patterns is easier than you think
This is a comprehensive guide to pyramid pattern programs with simple to understand algorithms.
Basics
Learning how to print a row & a column is very important for any pattern program.
1.Printing a row
Printing a new line so that every row is printed in a new line
for(int row=1;row<=n;row++){
printf("\n");
}
2.Reverse printing a row
Printing a row starting from n
till 1
while decrementing the row number.
for(int row=n;row>=1;row--){
printf("\n");
}
3.Printing a column
Inside each row, we will print multiple columns. To do that we need to print columns inside of each row.
// outer loop to print rows
for(int row=n;row>=1;row--){
// inner loop to print columns
for(int col=1;col<=n;col++){
printf("*");
}
printf("\n");
}
// this will print a square filled with stars
// * * * *
// * * * *
// * * * *
// * * * *
4.Printing as many columns as the row number
Example : 3 columns for 3rd row , 5 columns for 5th row , 1 column for 1st row.
So, for that we need to print columns till number of row running. Meaning , if iteration for row 2 is running then the columns loop should run till 2 times.
// inner loop to print columns
for(int col=1;col<=row;col++){
printf("*");
}
// this will print a triangle filled with stars
// *
// * *
// * * *
// * * * *
Triangles
Right-Angled Triangle #1
The simplest of all. This is the base of all the algorithms later in this article. So here , we print as many columns as the row number. So for example : 3 rows for 3rd row , 5 columns for 5th row like that.
(Optional) You can add some space after *
to make it look pretty !
Steps :
Print Rows till
n
elements.Print columns till
row
number.
// outer loop to print rows
for(int row=1;row<=n;row++){
// inner loop to print columns
for(int col=1;col<=row;col++){
printf("* ");
}
printf("\n");
}
Reverse of Right-Angled Triangle #1
This is just the reverse of the 1st triangle. Knowing this will later come handy when we print the reverse of any other triangles as well as shapes like diamond , hourglass.
Steps :
Print the Right Angled Triangle #1
Reverse the rows
// outer loop reversed
for(int row=n;row>=1;row--){
for(int col=1;col<=row;col++){
printf("* ");
}
printf("\n");
}
Isosceles Triangle & Importance of Spaces
This is very important program. Once you understand this, you will easily create diamond & hourglass shapes. This is where we introduce the concepts of leading spaces.
Leading spaces : These are the spaces before the stars (leading means before). This space shifts the triangle.
Trailing spaces : These are the spaces after each star (trailing means after). This space spaces the triangle.
What happens actually is ?
for the 1st row : 4 spaces (blue circles) & then 1 star with trailing space (violet circles).
for the 2nd row : 3 spaces & then 2 stars with trailing spaces in between each of them.
for the 3rd row : 2 spaces & then 3 stars with trailing spaces in between each of them.
for nth row :
n-row
spaces & n stars with trailing spaces for each of them.
So , the 1st row has 6 columns (4 leading spaces + 1 star + 1 trailing space) whereas 5th row (last row) has 10 columns(0 leading spaces + 5 stars with 5 trailing spaces in between them) . That means 5th column is at the middle of the figure. So the 1st row's star is at the middle of the figure.
Both the leading & trailing spaces gives the look of isosceles triangle!
Steps to print ▲ :
Print the Right-Angled Triangle #1
Before printing columns, print the leading spaces till
n-row
times.Then include spaces after each star.
for (int row=1;row<=n;row++){
// print leading spaces
for (int spaces=1;spaces <= n-row; spaces++){
printf(" "); // single space
}
// print stars
for (int col=1; col <= row; col++){
printf("* "); // trailing space added
}
// create new row
printf("\n");
}
Right Angled Triangle #2
Here also space plays a very important role. If you double the amount of leading spaces , then the triangle looks like the above image.
Steps to print ◢ :
Print the isosceles triangle
Double the amount of leading spaces.
for (int row=1;row<=n;row++){
// print leading spaces
for (int spaces=1;spaces <= n-row; spaces++){
printf(" "); // double space
}
// print stars
for (int col=1; col <= row; col++){
printf("* "); // trailing space added
}
// create new row
printf("\n");
}
Reverse of Isosceles Triangle & Right-Angled Triangle #2 : Just reverse the rows using the steps I showed above.
Combining of the above triangles to create different patterns
The above base triangles are enough to combine them to print multiple complex patterns.
Right Pascals Triangle
Steps :
Print the right-angled triangle #1 ◣
Print another ◣ & then reverse the rows to reverse it ◤
Remove the last row from any triangle to make it look prettier
#include <stdio.h>
int main()
{
int n = 6;
for(int row=1;row<=n;row++){
for(int col=1;col<=row;col++){
printf("*");
}
printf("\n");
}
for(int row=n-1;row>=1;row--){
for(int col=1;col<=row;col++){
printf("*");
}
printf("\n");
}
return 0;
}
K Pattern
Steps :
Print the reverse of right-angled triangle #1 ◤
Print the right-angled triangle #1 ◣
Remove the first row from any triangle to make it look prettier
#include <stdio.h>
int main()
{
int n = 6;
for(int row=n;row>=1;row--){
for(int col=1;col<=row;col++){
printf("*");
}
printf("\n");
}
for(int row=2;row<=n;row++){
for(int col=1;col<=row;col++){
printf("*");
}
printf("\n");
}
return 0;
}
Diamond
Steps :
Print the isosceles triangle ▲
Print the reverse of isosceles triangle ▼
Remove the last row from any triangle to make it look prettier
#include <stdio.h>
int main()
{
int n = 6;
// print ▲
for (int row=1;row<=n;row++){
// print leading spaces
for (int spaces=1;spaces <= n-row; spaces++){
printf(" "); // single space
}
// print stars
for (int col=1; col <= row; col++){
printf("* "); // trailing space added
}
// create new row
printf("\n");
}
// print ▼
for (int row=n-1;row>=1;row--){
// print leading spaces
for (int spaces=1;spaces <= n-row; spaces++){
printf(" "); // single space
}
// print stars
for (int col=1; col <= row; col++){
printf("* "); // trailing space added
}
// create new row
printf("\n");
}
return 0;
}
Hourglass
Steps :
Print the reverse of isosceles triangle ▼
Print the isosceles triangle ▲
Remove the 1st row from any triangle
#include <stdio.h>
int main()
{
int n = 6;
// print ▼
for (int row=n;row>=1;row--){
// print leading spaces
for (int spaces=1;spaces <= n-row; spaces++){
printf(" "); // single space
}
// print stars
for (int col=1; col <= row; col++){
printf("* "); // trailing space added
}
// create new row
printf("\n");
}
// print ▲
for (int row=2;row<=n;row++){
// print leading spaces
for (int spaces=1;spaces <= n-row; spaces++){
printf(" "); // single space
}
// print stars
for (int col=1; col <= row; col++){
printf("* "); // trailing space added
}
// create new row
printf("\n");
}
return 0;
}
Hollow Patterns
This means the 1st & last column of each row as well as all columns of first & last rows will be printed with stars & the middle columns will be printed as spaces.
Hollow Rectangles
Steps :
Print rows & columns till nth element
Check if col is 1st or last OR row is 1st or last
if yes , then print
*
else , print space
#include <stdio.h>
int main()
{
int n =5;
for (int row =1; row <= n; row++)
{
for (int col =1; col <= n; col++)
{
if(row==1 || col==1 || row==n|| col==n)
{
printf("*") ;
}
else{
printf(" ") ;
}
}
printf("\n");
}
}
Hollow Triangles
Steps :
Print ◣
Check if col is 1st or last OR row is 1st or last
if yes , then print
*
else , print space
// outer loop to print rows
for(int row=1;row<=n;row++){
// inner loop to print columns
for(int col=1;col<=row;col++){
if(row==1 || row==n || col==1 || col==row)
printf("* ");
else
printf(" "); //double spaces
}
printf("\n");
}
Hollow Diamond
Steps :
Print 🔷 like the way shown above
Then for each time printing columns Check if col is 1st or last
if yes , then print
*
else , print space
#include <stdio.h>
int main()
{
int n = 6;
// print ▲
for (int row=1;row<=n;row++){
// print leading spaces
for (int spaces=1;spaces <= n-row; spaces++){
printf(" "); // single space
}
// print stars
for (int col=1; col <= row; col++){
if(col==1 || col==row)
printf("* ");
else
printf(" "); //double spaces
}
// create new row
printf("\n");
}
// print ▼
for (int row=n-1;row>=1;row--){
// print leading spaces
for (int spaces=1;spaces <= n-row; spaces++){
printf(" "); // single space
}
// print stars
for (int col=1; col <= row; col++){
if(col==1 || col==row)
printf("* ");
else
printf(" "); //double spaces
}
// create new row
printf("\n");
}
return 0;
}
Conclusion
You can try to print more Pyramid patterns after understanding the concepts. Here is a simple challenge for you! Can you print Hollow Hourglass ?
Do let me know your feedback on this article & how did you solve this challenge ..