Program to print the given H Pattern

27 Apr, 2021
Given an integer N, the task is to print the Alphabet H Pattern as given below:  
1       N
2       *
3       3
*       2
N * 3 2 1
*       2
3       3
2       *    
1       N
Examples:  
Input: N = 3
Output: 
1   3
2   2
3 2 1
2   2
1   3

Input: N = 4
Output: 
1     4
2     3
3     2
4 3 2 1
3     2
2     3
1     4
 
Approach:  
Print the Left value and leave 2 * (index – 1) blank spaces & print Right value.
Print the Nth row with N to 1 number.
Repeat step one for (2 * N) – 1 time to print the desired H pattern.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Function to print the desired// Alphabet H Patternvoid alphabetPattern(int N){     // Declaring the values of left,    // middle, right side    int left = 0, middle = N - 1, right = N + 1;     // Main Row Loop    for (int row = 0; row < 2 * N - 1; row++) {         // Condition for the left Values        if (row < N)            cout << ++left;        else            cout << --left;         // Loop for the middle values        for (int col = 1; col < N - 1; col++) {             // Condition for the middleValues            if (row != N - 1)                 // Two spaces for perfect alignment                cout << " "                     << " ";            else                cout << " " << middle--;        }         // Condition for the right Values        if (row < N)            cout << " " << --right;        else            cout << " " << ++right;        cout << endl;    }} // Driver Codeint main(){    // Size of the Pattern    int N = 4;     alphabetPattern(N);     return 0;}
// Java implementation of the approachclass GFG{// Function to print the desired// Alphabet H Patternstatic void alphabetPattern(int N){     // Declaring the values of left,    // middle, right side    int left = 0, middle = N - 1, right = N + 1;     // Main Row Loop    for (int row = 0; row < 2 * N - 1; row++) {         // Condition for the left Values        if (row < N)            System.out.print( ++left);        else            System.out.print(--left);         // Loop for the middle values        for (int col = 1; col < N - 1; col++) {             // Condition for the middleValues            if (row != N - 1)                 // Two spaces for perfect alignment            System.out.print( "  ");                                 else                System.out.print( " " +middle--);        }         // Condition for the right Values        if (row < N)            System.out.print( " "  +--right);        else            System.out.print( " " + ++right);        System.out.println();    }} // Driver Code    public static void main(String[] args) {     // Size of the Pattern    int N = 4;     alphabetPattern(N);// This code is contributed by Rajput-Ji }}
# Python3 implementation of the approach # Function to print the desired# Alphabet H Patterndef alphabetPattern(N):     # Declaring the values of left,    # middle, right side    left, middle, right = 0, N - 1, N + 1     # Main Row Loop    for row in range(0, 2 * N - 1):         # Condition for the left Values        if row < N:            left += 1            print(left, end = "")        else:            left -= 1            print(left, end = "")         # Loop for the middle values        for col in range(1, N - 1):             # Condition for the middleValues            if row != N - 1:                 # Two spaces for perfect alignment                print(" ", end = " ")                             else:                print(" " + str(middle), end = "")                middle -= 1         # Condition for the right Values        if row < N:            right -= 1            print(" " + str(right), end = "")        else:            right += 1            print(" " + str(right), end = "")                 print() # Driver Codeif __name__ == "__main__":     # Size of the Pattern    N = 4    alphabetPattern(N) # This code is contributed by Rituraj Jain
// C# implementation of the approachusing System; class GFG{     // Function to print the desired// Alphabet H Patternstatic void alphabetPattern(int N){     // Declaring the values of left,    // middle, right side    int left = 0, middle = N - 1, right = N + 1;     // Main Row Loop    for (int row = 0; row < 2 * N - 1; row++)    {         // Condition for the left Values        if (row < N)            Console.Write( ++left);        else            Console.Write(--left);         // Loop for the middle values        for (int col = 1; col < N - 1; col++)        {             // Condition for the middleValues            if (row != N - 1)                 // Two spaces for perfect alignment            Console.Write( " ");                                 else                Console.Write( " " + middle--);        }         // Condition for the right Values        if (row < N)            Console.Write( " " + --right);        else            Console.Write( " " + ++right);        Console.WriteLine();    }} // Driver Codepublic static void Main(String[] args){     // Size of the Pattern    int N = 4;    alphabetPattern(N);}} // This code is contributed by// PrinciRaj1992
<?php// PHP implementation of the approach // Function to print the desired// Alphabet H Patternfunction alphabetPattern($N){     // Declaring the values of left,    // middle, right side    $left = 0;    $middle = $N - 1;    $right = $N + 1;     // Main Row Loop    for ($row = 0; $row < 2 * $N - 1; $row++)    {         // Condition for the left Values        if ($row < $N)            echo (++$left);        else            echo (--$left);         // Loop for the middle values        for ($col = 1; $col < $N - 1; $col++)        {             // Condition for the middleValues            if ($row != $N - 1)                 // Two spaces for perfect alignment                echo " "." ";            else                echo " ".($middle--);        }         // Condition for the right Values        if ($row < $N)            echo " ".(--$right);        else            echo " ".(++$right);        echo "\n";    }}     // Driver Code     // Size of the Pattern    $N = 4;     alphabetPattern($N);      // This code is contributed by mits?>
<script>       // JavaScript implementation      // of the approach       // Function to print the desired      // Alphabet H Pattern      function alphabetPattern(N)      {        // Declaring the values of left,        // middle, right side        var left = 0,          middle = N - 1,          right = N + 1;         // Main Row Loop        for (var row = 0; row < 2 * N - 1; row++)        {          // Condition for the left Values          if (row < N) {            ++left;            document.write(left);          } else {            --left;            document.write(left);          }           // Loop for the middle values          for (var col = 1; col < N - 1; col++)          {            // Condition for the middleValues            if (row != N - 1)              // Two spaces for perfect alignment       document.write("  " + "  ");            else {              document.write("  " + middle);              middle--;            }          }           // Condition for the right Values          if (row < N) {            --right;            document.write("  " + right);          } else {            ++right;            document.write("  " + right);          }          document.write("<br>");        }      }       // Driver Code             // Size of the Pattern      var N = 4;             alphabetPattern(N);       </script>
1     4
2     3
3     2
4 3 2 1
3     2
2     3
1     4
 
Rajput-Ji
rituraj_jain
princiraj1992
Mithun Kumar
rdtank
pattern-printing
C++ Programs
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.