Pattern printing in  C++

Pattern printing in  C++

In this tutorial, we will learn the following programs;

  1. Write a C++ program to show the inverted hollow star pyramid pattern printing.
  2. C++ program to show the pyramid filled with stars pattern printing.

Write a C++ program to show the inverted hollow star pyramid pattern printing

//C++ program to show the inverted hollow star pyramid

#include<iostream>
using namespace std;
int main()
{
   int total_rows, i, j, space_between;
   cout << "Enter total number of total_rows you want to insert: ";
   cin >> total_rows;
   for(i = total_rows; i >= 1; i--)
   {
      //for loop to put space between stars
      for (space_between = i; space_between < total_rows; space_between++)
         cout << " ";

      //for loop to print the star in pyramid
      for(j = 1; j <= 2 * i - 1; j++)
      {
         if(i == total_rows || j == 1 || j == 2*i - 1)
            cout << "*";
         else
            cout << " ";
      }
      cout << "\n";
   }
   }

Output

C++ program to show the inverted hollow star pyramid - C Plus Plus
Figure: C++ program to show the inverted hollow star pyramid – C Plus Plus

C++ program to show the pyramid filled with stars pattern printing

//C++ program to show the  pyramid filled with stars
#include <iostream>
using namespace std;
int main()
{
   int total_rows, i, j, space_between;
   cout << "Please Enter number of total_rows: ";
   cin >> total_rows;

   for(i = 1; i <= total_rows; i++)
   {
      //for loop for displaying space_between the stars
      for(space_between = i; space_between < total_rows; space_between++)
      {
         cout << " ";
      }
      //Code of for loop to show  star equal to the row number
      for(j = 1; j <= (2 * i - 1); j++)
      {
         cout << "*";
      }

      cout << "\n";
   }

   return 0;
}

Output

C++ program to display full star pyramid - C Plus Plus
Figure: C++ program to display full star pyramid – C Plus Plus
Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.