Program to print the given Z Pattern
Given an integer N, the task is to print the Alphabet Z Pattern as given below:
1 2 3 * * * N
*
*
*
3
2
1 2 3 * * * N
Examples:
Input: N = 5
Output:
1 2 3 4 5
4
3
2
1 2 3 4 5
Input: N = 4
Output:
1 2 3 4
3
2
1 2 3 4
Approach:
- Print the first row with 1 to N numbers.
- Then from 2nd to (N-1)th row, print 2 * (N – index – 1) times blank spaces followed by the ending element which is index – 1.
- Print the last row with 1 to N numbers.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void alphabetPattern( int N)
{
int index, side_index, size;
int Top = 1, Bottom = 1, Diagonal = N - 1;
for (index = 0; index < N; index++)
cout << Top++ << " " ;
cout << endl;
for (index = 1; index < N - 1; index++) {
for (side_index = 0; side_index < 2 * (N - index - 1);
side_index++)
cout << " " ;
cout << Diagonal--;
cout << endl;
}
for (index = 0; index < N; index++)
cout << Bottom++ << " " ;
}
int main()
{
int N = 5;
alphabetPattern(N);
return 0;
}
|
C
#include <stdio.h>
void alphabet_Z_Pattern( int N)
{
int index, side_index, size;
int Top = 1, Bottom = 1, Diagonal = N - 1;
for (index = 0; index < N; index++)
printf ( "%d " , Top++);
printf ( "\n" );
for (index = 1; index < N - 1; index++) {
for (side_index = 0; side_index < 2 * (N - index - 1);
side_index++)
printf ( " " );
printf ( "%d" , Diagonal--);
printf ( "\n" );
}
for (index = 0; index < N; index++)
printf ( "%d " , Bottom++);
}
int main()
{
int N = 5;
alphabet_Z_Pattern(N);
return 0;
}
|
Java
class GFG
{
static void alphabetPattern( int N)
{
int index, side_index;
int Top = 1 , Bottom = 1 , Diagonal = N - 1 ;
for (index = 0 ; index < N; index++)
System.out.print(Top++ + " " );
System.out.println();
for (index = 1 ; index < N - 1 ; index++)
{
for (side_index = 0 ;
side_index < 2 * (N - index - 1 );
side_index++)
System.out.print( " " );
System.out.print(Diagonal--);
System.out.println();
}
for (index = 0 ; index < N; index++)
System.out.print(Bottom++ + " " );
}
public static void main(String args[])
{
int N = 5 ;
alphabetPattern(N);
}
}
|
Python3
def alphabetPattern(N):
Top, Bottom, Diagonal = 1 , 1 , N - 1
for index in range (N):
print (Top, end = ' ' )
Top + = 1
print ()
for index in range ( 1 , N - 1 ):
for side_index in range ( 2 * (N - index - 1 )):
print ( ' ' , end = '')
print (Diagonal, end = '')
Diagonal - = 1
print ()
for index in range (N):
print (Bottom, end = ' ' )
Bottom + = 1
N = 5
alphabetPattern(N)
|
C#
using System;
class GFG
{
static void alphabetPattern( int N)
{
int index, side_index;
int Top = 1, Bottom = 1, Diagonal = N - 1;
for (index = 0; index < N; index++)
Console.Write(Top++ + " " );
Console.WriteLine();
for (index = 1; index < N - 1; index++)
{
for (side_index = 0; side_index < 2 * (N - index - 1);
side_index++)
Console.Write( " " );
Console.Write(Diagonal--);
Console.WriteLine();
}
for (index = 0; index < N; index++)
Console.Write(Bottom++ + " " );
}
public static void Main()
{
int N = 5;
alphabetPattern(N);
}
}
|
PHP
<?php
function alphabetPattern( $N )
{
$index ; $side_index ; $size ;
$Top = 1; $Bottom = 1; $Diagonal = $N - 1;
for ( $index = 0; $index < $N ; $index ++)
echo $Top ++ . " " ;
echo "\n" ;
for ( $index = 1; $index < $N - 1; $index ++)
{
for ( $side_index = 0;
$side_index < 2 * ( $N - $index - 1);
$side_index ++)
echo " " ;
echo $Diagonal --;
echo "\n" ;
}
for ( $index = 0; $index < $N ; $index ++)
echo $Bottom ++ . " " ;
}
$N = 5;
alphabetPattern( $N );
|
Javascript
<script>
function alphabetPattern(N)
{
var index, side_index, size;
var Top = 1,
Bottom = 1,
Diagonal = N - 1;
for (index = 0; index < N; index++) {
document.write(Top + " " );
Top++;
}
document.write( "<br>" );
for (index = 1; index < N - 1; index++) {
for (side_index = 0; side_index <
2 * (N - index - 1); side_index++)
document.write( " " );
document.write(Diagonal);
Diagonal--;
document.write( "<br>" );
}
for (index = 0; index < N; index++) {
document.write(Bottom + " " );
Bottom++;
}
}
var N = 5;
alphabetPattern(N);
</script>
|
Output:
1 2 3 4 5
4
3
2
1 2 3 4 5
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...