Program to Print Pyramid Patterns with Asterisk (*) Character

A very simple program to print pyramid pattern with star character using different looping techniques. This type of challenges are best to understand the concept of nested loops.

Generating character patterns in C/C++ is a very effective way understand nested loops , their structures, related algorithms, concepts and the language itself. It is one of the most common of basic C++ coding questions related to loops. In this post, you'll learn about various character patterns you can draw using nested loops.

The character patterns like half pyramids, full pyramids, inverted pyramids etc, make it easier to grasp the very concept of loops and all the magic it contains. There are various such patterns that have been listed here to help you understand the concept in depth. We are going to draw the following list of patterns:

Purpose

Write a program to print a half pyramid with star (*) characters

#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
{
   int i, j, rows;

   printf(" Enter the desired number of rows: ");

   scanf("%d", &rows);

   printf("\n");

   for (i = 0; i <= rows; i++) // The Outer Loop;
   {
      for (j = 0; j < i; j++) // The inner loop runs as many times as the value of the variable i
      {
         printf("* "); // Prints the star character
      }

      printf("\n");
   }

   getch(); // Pauses the program until a keypress

   return 0;
}
Output
Enter the desired number of rows: 5
*
* *
* * *
* * * *
* * * * *

Explanation

The pyramid pattern is a beautiful depiction of what you can achieve with nested loops. First of all we are asking the user to input a number which corresponds to the number of rows of the star/astesrisk (*) character. You can see that the inner loop run as many times as the current value of variable i in the parent loop. We are then printing the star (*) character using printf(), you can use cout as well by including the iostream header file. This is how the pattern is generated. Now, lets tweak this pattern and see what else can we come up with.

Inverted half pyramid

If you're wondering whether we could tweak the loops to print a inverted half pyramid of asterisk characters then yes let understand how we could do that. Let's take a look at the code below:

#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
{
   int i, j, rows;

   printf(" Enter the desired number of rows: ");

   scanf("%d", &rows);

   printf("\n");

   for (i = rows; i > 0; i--) // The Outer Loop;
   {
      for (j = 0; j < i; j++) // The inner loop runs as many times as the value of the variable i
      {
         printf("* "); // Prints the star character
      }

      printf("\n");
   }

   getch(); // Pauses the program until a keypress

   return 0;
}
Output
Enter the desired number of rows: 5
* * * * *
* * * *
* * *
* *
*

Explanation

Now, as you may observe we have successfully inverted the pyramid pattern. There a very tiny little tweak we did in the code above, we modified the outer loop from this for (i = 0; i <= rows; i++) to for (i = rows; i > 0; i--) and that's it. We just told the outer loop to start from the highest number and reduce down to zero. As the inner loop is dependent upon the outer loop, it automatically prints the largest number of star characters first.

Again, if you're wondering what other ways we can tweak the pattern, then lets mirror or laterally invert the printed half portion of both the pyramids above.

Generating Right Half of the Pyramid

In both the code examples above, we printed only the half portion of the pyramid. In this sample, we'll print the other half of the pyramid.

#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
{
   int i, j, k, rows;

   printf(" Enter the desired number of rows: ");

   scanf("%d", &rows);

   printf("\n");

   for (i = 0; i <= rows; i++) // The Outer Loop;
   {
      for (j = rows; j > i; j--) // The first inner loop
      {
         printf(" "); // Prints a blank space character
      }

      for (k = 0; k < i; k++) // The second inner loop
      {
         printf("*"); // Prints the star character
      }

      printf("\n");
   }

   getch(); // Pauses the program until a keypress

   return 0;
}
Output

    Enter the desired number of rows: 5
           *
         * *
       * * *
     * * * *
   * * * * *

Explanation

In this pattern we generated above, you'll notice we have used two for loops inside the outer loop. The first for loop simply prints a blank space character which pushes the star character further as many times the first inner loop runs.

The second inner loop prints the star character normally as the very first example in this lesson. Combining these two loops results in the pattern displayed above. Again, lets follow on to ther inverted right half of the pyramid.

Generating Right half of the Inverted Pyramid

Above we generated half of the first pyramid, lets generate the Inverted half of the pyramid. This time again we'll tweak the code above to output the desired star pattern.

#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
{
   int i, j, k, rows;

   printf(" Enter the desired number of rows: ");

   scanf("%d", &rows);

   printf("\n");

   for (i = 0; i <= rows; i++) // The Outer Loop;
   {
      for (j = 0; j <= i; j++) // The first inner loop
      {
         printf("  "); // Prints a blank space character
      }

      for (k = rows; k > i; k--) // The second inner loop
      {
         printf(" *"); // Prints the star character
      }

      printf("\n");
   }

   getch(); // Pauses the program until a keypress

   return 0;
}
Output
Enter the desired number of rows: 5      
* * * * *
  * * * *
    * * *
      * *
        *

Conclusion

In this lesson you learnt how to print various patterns, (triangles, pyramids) with the asterisk (*) character. You can now combine these patterns and code above to produce a full triangular pattern. In the next lesson we'd see similar patterns but using numbers.