Open In App

Program to print the given H Pattern

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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;
}


Java




// 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
  
}
}


Python3




# 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


C#




// 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


PHP




<?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
?>


Javascript




<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>


Output: 

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

 

Time complexity: O(N2) for given input N

Auxiliary Space: O(1)



Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads