Program to print a inverse pyramid character pattern
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; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 = 9pyramid(n) # This code is contributed by "Sharad_Bhardwaj". |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
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
Recommended Posts:
- Program to print pyramid pattern
- Program to print pyramid pattern
- Program to Print Pyramid Pattern using numbers
- Program to print Inverse Diamond pattern
- Program to print hollow pyramid and diamond pattern
- Program to print reverse character bridge pattern
- Print the following pyramid pattern
- Print the pyramid pattern with given height and minimum number of stars
- Program to print Even Odd Number Pyramid
- Program to print the given H Pattern
- Program to print the pattern 'G'
- Program to print the pattern "GFG"
- Program to print the pattern ‘D’
- Program to print the given Z Pattern
- Program to print pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar



