Open In App

Program to print a inverse pyramid character pattern

Last Updated : 20 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Given a positive integer n, print the inverse pyramid pattern upto n rows as shown in the examples.
Examples : 
 

Input : 4
Output :

A B C D D C B A
  A B C C B A
    A B B A
      A A
 
Input : 6
Output :

 A B C D E F F E D C B A
   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


 


Below is the implementation for the pattern: 
 

C++
// C++ code to print inverse 
// pyramid pattern
#include <bits/stdc++.h>
using namespace std;

// function to print the
// inverse pyramid pattern
void pyramid(int n)
{
    int i, j, num, gap;

    // outer loop to handle number
    // of rows n in this case
    for (i = n; i >= 1; i--) {
        
        // inner loop to create right triangle
        // gaps on left side of pyramid
        for (gap = n - 1; gap >= i; gap--) {
            cout<<" ";
            cout<<" ";
        }
        
        // initializing value corresponding 
        // to 'A' ASCII value
        num = 'A';

        // loop to print characters on
        // left side of pyramid
        for (j = 1; j <= i; j++) {
            cout << (char) num++ <<" ";
        }
        
        // loop to print characters on
        // right side of pyramid
        for (j = i - 1; j >= 0; j--) {
            cout << (char) --num <<" ";
        }
        cout<<"\n";
    }
}

// Driver function
int main()
{
    int n = 9;
    pyramid(n);
    return 0;
}
Java
// Java code for Inverse Pyramid
import java.util.*;

class GFG {
    
    // function for inverse pyramid print
    static void pyramid(int n)
    {
        int i, j, num, gap;
    
        // outer loop to handle number
        // of rows n in this case
        for (i = n; i >= 1; i--) {
            
            // inner loop to create right triangle
            // gaps on left side of pyramid
            for (gap = n - 1; gap >= i; gap--) {
                System.out.print(" ");
                System.out.print(" ");
            }
            
            // initializing value corresponding 
            // to ASCII value of 'A' 
            num = 'A';
    
            // loop to print characters on
            // left side of pyramid
            for (j = 1; j <= i; j++) {
                System.out.print((char)num++ + " ");
            }
            
            // loop to print characters on
            // right side of pyramid
            for (j = i - 1; j >= 0; j--) {
                System.out.print((char)--num + " ");
            }
            
            System.out.println("");
        }
    }
    
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n = 9;
        pyramid(n);
    
    }
}

// 
Python3
# Python3 code to print inverse 
# pyramid pattern

# function to print the following
# inverse pyramid pattern
def pyramid( n ):
    
    # outer loop to handle number
    # of rows n in this case
    for i in range(n, 0, -1):
        
        # inner loop to create right triangle
        # gaps on left side of pyramid
        for gap in range(n-1, i-1, -1):
            print(" ", end = '')
            print(" ", end = '')
        
        # initializing value corresponding
        # to 'A' ASCII value
        num = ord('A')
        
        # loop to print characters on
        # left side of pyramid
        for j in range(1, i+1):
            print(chr(num), end = ' ')
            num += 1
            
        # loop to print characters on
        # right side of pyramid
        for j in range(i - 1, -1, -1):
            num -= 1
            print(chr(num), end = ' ')
        
        print("\n", end = '')

# Driver Code
n = 9
pyramid(n)

# This code is contributed by "Sharad_Bhardwaj".
C#
 //C# code for Inverse Pyramid
using System;

class GFG {
    
    // function for inverse pyramid print
    static void pyramid(int n)
    {
        int i, j, num, gap;
    
        // outer loop to handle number
        // of rows n in this case
        for (i = n; i >= 1; i--) {
            
            // inner loop to create right triangle
            // gaps on left side of pyramid
            for (gap = n - 1; gap >= i; gap--) {
                Console.Write(" ");
                Console.Write(" ");
            }
            
            // initializing value corresponding 
            // to ASCII value of 'A' 
            num = 'A';
    
            // loop to print characters on
            // left side of pyramid
            for (j = 1; j <= i; j++) {
                Console.Write((char)num++ + " ");
            }
            
            // loop to print characters on
            // right side of pyramid
            for (j = i - 1; j >= 0; j--) {
              Console.Write((char)--num + " ");
            }
            
            Console.WriteLine("");
        }
    }
    
    /* Driver program to test above function */
    public static void Main()
    {
        int n = 9;
        pyramid(n);
    
    }
}

// This article is contributed by vt_m.
PHP
<?php
// PHP implementation to print 
// inverse pyramid pattern

// function to print the
// inverse pyramid pattern
function pyramid($n)
{
    // outer loop to handle number
    // of rows n in this case
    for ($i = $n; $i >= 1; $i--) 
    {
        
        // inner loop to create 
        // right triangle gaps on 
        // left side of pyramid
        for ($gap = $n - 1; $gap >= $i; 
                            $gap--) 
        {
            echo"  ";
        }
        
        // initializing value corresponding 
        // to 'A' ASCII value is 65
        $num = 65;

        // loop to print characters on
        // left side of pyramid
        for ($j = 1; $j <= $i; $j++) 
        {
            echo chr($num++)." ";
        }
        
        // loop to print characters on
        // right side of pyramid
        for ($j = $i - 1; $j >= 0; $j--) 
        {
            echo chr(--$num)." ";
        }
        echo"\n";
    }
}

// Driver Code
$n = 9;
pyramid($n);

// This code is contributed by mits 
?>
JavaScript
<script>

      // JavaScript code to print inverse
      // pyramid pattern

      // function to print the
      // inverse pyramid pattern
      function pyramid(n) 
      {
        var i, j, num, gap;

        // outer loop to handle number
        // of rows n in this case
        for (i = n; i >= 1; i--) {
          // inner loop to create right triangle
          // gaps on left side of pyramid
          for (gap = n - 1; gap >= i; gap--) {
            document.write("&nbsp;&nbsp;&nbsp;&nbsp;");
          }

          // initializing value corresponding
          // to 'A' ASCII value
          num = "A".charCodeAt(0);

          // loop to print characters on
          // left side of pyramid
          for (j = 1; j <= i; j++) {
            document.write(String.fromCharCode(num++) 
            + "&nbsp;&nbsp;");
          }

          // loop to print characters on
          // right side of pyramid
          for (j = i - 1; j >= 0; j--) {
            document.write(String.fromCharCode(--num)
            + "&nbsp;&nbsp;");
          }
          document.write("<br>");
        }
      }

      // Driver function
      var n = 9;
      pyramid(n);
      
</script>
    

Output
A B C D E F G H I I H G F E D C B A 
  A B C D E F G H H G F E D C B A 
    A B C D E F G G F E D C B A 
      A B C D E F F E D C B A 
        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 

Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.


Next Article

Similar Reads