Program to print the given H Pattern

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 numbers.
  • Repeat step one for (2 * N) – 1 times to print the desired H pattern.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to print the desired
// Alphabet H Pattern
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)
            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 Code
int main()
{
    // Size of the Pattern
    int N = 4;
  
    alphabetPattern(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
// Function to print the desired
// Alphabet H Pattern
static 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
  
}
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to print the desired 
# Alphabet H Pattern 
def 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 Code 
if __name__ == "__main__":
  
    # Size of the Pattern 
    N = 4
    alphabetPattern(N) 
  
# This code is contributed by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG
{
      
// Function to print the desired
// Alphabet H Pattern
static 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 Code
public static void Main(String[] args) 
{
  
    // Size of the Pattern
    int N = 4;
    alphabetPattern(N);
}
}
  
// This code is contributed by 
// PrinciRaj1992

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to print the desired
// Alphabet H Pattern
function 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
?>

chevron_right


Output:

1     4
2     3
3     2
4 3 2 1
3     2
2     3
1     4


My Personal Notes arrow_drop_up


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.