Program to print an inverse pyramid character pattern in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to print an inverse pyramid character pattern.

For this we will be provided with the number of rows containing in the inverted pyramid triangle. Our task is to print the alphabets in the given number of rows to develop the shape of an inverse pyramid.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//printing the inverse pyramid pattern
void inv_pyramid(int n){
   int i, j, num, gap;
   for (i = n; i >= 1; i--) {
      for (gap = n - 1; gap >= i; gap--) {
         cout<<" ";
         cout<<" ";
      }
      num = 'A';
      for (j = 1; j <= i; j++) {
         cout << (char) num++ <<" ";
      }
      for (j = i - 1; j >= 0; j--) {
         cout << (char) --num <<" ";
      }
      cout<<"\n";
   }
}
int main(){
   int n = 5;
   inv_pyramid(n);
   return 0;
}

Output

A B C D E E D C B A
 A B C D D C B A
  A B C C B A
   A B B A
    A A
raja
Published on 19-Dec-2019 15:17:41
Advertisements