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); } } // This article is contributed by Gitanjali. |
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( " " ); } // 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++) + " " ); } // loop to print characters on // right side of pyramid for (j = i - 1; j >= 0; j--) { document.write(String.fromCharCode(--num) + " " ); } document.write( "<br>" ); } } // Driver function var n = 9; pyramid(n); </script> |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!