C++ Program to print Inverted Pyramid and Hollow Inverted Pyramid Pattern

 

C++ Program to print Inverted Pyramid and Hollow Inverted Pyramid Pattern, Inverted Pyramid Pattern, Hollow Inverted Pyramid Pattern, Pattern Problem, pattern in c++

(i) Inverted Pyramid

Program:

#include <iostream>
using namespace std;

int main ()
{
  int n;
  cout << "Enter number of rows: ";
  cin >> n; //input number of rows to be printed

  for (int i = 0; i < n; i++)   //loop to terminate line
    {
      for (int j = 0; j < i; j++)   //loop to print spaces
	   cout << " ";

      for (int k = 0; k < n - i; k++)   //loop to print *
	    cout << "*" << " ";

      cout << endl; //after printing space and * in a row change the line to print in next row
    }
  return 0;
}

Output:

* * * * *
 * * * *
  * * *
   * *
    *

(ii) Hollow Inverted Pyramid

Program:

#include <iostream>
using namespace std;

int main ()
{
  int n;
  cout << "Enter number of rows: ";
  cin >> n;			//input number of rows to be printed

  for (int i = 0; i < n; i++)	//loop to terminate line
    {
      for (int j = 0; j < i; j++)	//loop to print spaces
	    cout << " ";

      for (int k = 0; k < n - i; k++)	//loop to print *
	    {
	        if (k == 0 || k == n - i - 1 || i == 0 || i == n - 1)//print * at boundaries and spaces at non boundary index
	            cout << "*" << " ";

	        else
	            cout << "  ";
	    }
      cout << endl;		//after printing space and * in a row change the line to print in next row
    }
  return 0;
}

Output:

* * * * *
 *     *
  *   *
   * *
    *
C++ Program to print Inverted Pyramid and Hollow Inverted Pyramid Pattern C++ Program to print Inverted Pyramid and Hollow Inverted Pyramid Pattern Reviewed by Beast Coder on February 14, 2021 Rating: 5

No comments:

Powered by Blogger.