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 number.
- Repeat step one for (2 * N) – 1 time to print the desired H pattern.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void alphabetPattern( int N)
{
int left = 0, middle = N - 1, right = N + 1;
for ( int row = 0; row < 2 * N - 1; row++) {
if (row < N)
cout << ++left;
else
cout << --left;
for ( int col = 1; col < N - 1; col++) {
if (row != N - 1)
cout << " "
<< " " ;
else
cout << " " << middle--;
}
if (row < N)
cout << " " << --right;
else
cout << " " << ++right;
cout << endl;
}
}
int main()
{
int N = 4;
alphabetPattern(N);
return 0;
}
|
Java
class GFG
{
static void alphabetPattern( int N)
{
int left = 0 , middle = N - 1 , right = N + 1 ;
for ( int row = 0 ; row < 2 * N - 1 ; row++) {
if (row < N)
System.out.print( ++left);
else
System.out.print(--left);
for ( int col = 1 ; col < N - 1 ; col++) {
if (row != N - 1 )
System.out.print( " " );
else
System.out.print( " " +middle--);
}
if (row < N)
System.out.print( " " +--right);
else
System.out.print( " " + ++right);
System.out.println();
}
}
public static void main(String[] args) {
int N = 4 ;
alphabetPattern(N);
}
}
|
Python3
def alphabetPattern(N):
left, middle, right = 0 , N - 1 , N + 1
for row in range ( 0 , 2 * N - 1 ):
if row < N:
left + = 1
print (left, end = "")
else :
left - = 1
print (left, end = "")
for col in range ( 1 , N - 1 ):
if row ! = N - 1 :
print ( " " , end = " " )
else :
print ( " " + str (middle), end = "")
middle - = 1
if row < N:
right - = 1
print ( " " + str (right), end = "")
else :
right + = 1
print ( " " + str (right), end = "")
print ()
if __name__ = = "__main__" :
N = 4
alphabetPattern(N)
|
C#
using System;
class GFG
{
static void alphabetPattern( int N)
{
int left = 0, middle = N - 1, right = N + 1;
for ( int row = 0; row < 2 * N - 1; row++)
{
if (row < N)
Console.Write( ++left);
else
Console.Write(--left);
for ( int col = 1; col < N - 1; col++)
{
if (row != N - 1)
Console.Write( " " );
else
Console.Write( " " + middle--);
}
if (row < N)
Console.Write( " " + --right);
else
Console.Write( " " + ++right);
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int N = 4;
alphabetPattern(N);
}
}
|
PHP
<?php
function alphabetPattern( $N )
{
$left = 0;
$middle = $N - 1;
$right = $N + 1;
for ( $row = 0; $row < 2 * $N - 1; $row ++)
{
if ( $row < $N )
echo (++ $left );
else
echo (-- $left );
for ( $col = 1; $col < $N - 1; $col ++)
{
if ( $row != $N - 1)
echo " " . " " ;
else
echo " " .( $middle --);
}
if ( $row < $N )
echo " " .(-- $right );
else
echo " " .(++ $right );
echo "\n" ;
}
}
$N = 4;
alphabetPattern( $N );
?>
|
Javascript
<script>
function alphabetPattern(N)
{
var left = 0,
middle = N - 1,
right = N + 1;
for ( var row = 0; row < 2 * N - 1; row++)
{
if (row < N) {
++left;
document.write(left);
} else {
--left;
document.write(left);
}
for ( var col = 1; col < N - 1; col++)
{
if (row != N - 1)
document.write( " " + " " );
else {
document.write( " " + middle);
middle--;
}
}
if (row < N) {
--right;
document.write( " " + right);
} else {
++right;
document.write( " " + right);
}
document.write( "<br>" );
}
}
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
Share your thoughts in the comments
Please Login to comment...