Open In App

Program to print the given Z Pattern

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to print the desired
// Alphabet Z Pattern
void alphabetPattern(int N)
{
  
    int index, side_index, size;
  
    // Declaring the values of Right,
    // Left and Diagonal values
    int Top = 1, Bottom = 1, Diagonal = N - 1;
  
    // Loop for printing the first row
    for (index = 0; index < N; index++)
        cout << Top++ << " ";
  
    cout << endl;
  
    // Main Loop for the rows from (2 to n-1)
    for (index = 1; index < N - 1; index++) {
  
        // Spaces for the diagonals
        for (side_index = 0; side_index < 2 * (N - index - 1);
             side_index++)
            cout << " ";
  
        // Printing the diagonal values
        cout << Diagonal--;
  
        cout << endl;
    }
  
    // Loop for printing the last row
    for (index = 0; index < N; index++)
        cout << Bottom++ << " ";
}
  
// Driver Code
int main()
{
    // Number of rows
    int N = 5;
  
    alphabetPattern(N);
  
    return 0;
}


C




// C implementation of the approach
#include <stdio.h>
  
// Function to print the desired
// Alphabet Z Pattern
void alphabet_Z_Pattern(int N)
{
    int index, side_index, size;
  
    // Declaring the values of Right,
    // Left and Diagonal values
    int Top = 1, Bottom = 1, Diagonal = N - 1;
  
    // Loop for printing the first row
    for (index = 0; index < N; index++)
        printf("%d ", Top++);
  
    printf("\n");
  
    // Main Loop for the rows from (2 to n-1)
    for (index = 1; index < N - 1; index++) {
  
        // Spaces for the diagonals
        for (side_index = 0; side_index < 2 * (N - index - 1);
             side_index++)
            printf(" ");
  
        // Printing the diagonal values
        printf("%d", Diagonal--);
  
        printf("\n");
    }
  
    // Loop for printing the last row
    for (index = 0; index < N; index++)
        printf("%d ", Bottom++);
}
  
// Driver Code
int main()
{
    // Size of the Pattern
    int N = 5;
  
    alphabet_Z_Pattern(N);
  
    return 0;
}


Java




// Java implementation of the approach
  
class GFG
{
// Function to print the desired
// Alphabet Z Pattern
static void alphabetPattern(int N)
{
  
    int index, side_index;
  
    // Declaring the values of Right,
    // Left and Diagonal values
    int Top = 1, Bottom = 1, Diagonal = N - 1;
  
    // Loop for printing the first row
    for (index = 0; index < N; index++)
        System.out.print(Top++ + " ");
  
    System.out.println();
  
    // Main Loop for the rows from (2 to n-1)
    for (index = 1; index < N - 1; index++) 
    {
  
        // Spaces for the diagonals
        for (side_index = 0
             side_index < 2 * (N - index - 1);
            side_index++)
            System.out.print(" ");
  
        // Printing the diagonal values
        System.out.print(Diagonal--);
  
        System.out.println();
    }
  
    // Loop for printing the last row
    for (index = 0; index < N; index++)
        System.out.print(Bottom++ + " ");
}
  
// Driver Code
public static void main(String args[])
{
    // Number of rows
    int N = 5;
  
    alphabetPattern(N);
}
}
  
// This code is contributed
// by Akanksha Rai


Python3




# Python 3 implementation of the approach 
  
# Function to print the desired 
# Alphabet Z Pattern 
def alphabetPattern(N):
  
    # Declaring the values of Right, 
    # Left and Diagonal values 
    Top, Bottom, Diagonal = 1, 1, N - 1
  
    # Loop for printing the first row 
    for index in range(N):
        print(Top, end = ' ')
        Top += 1
    print()
  
    # Main Loop for the rows from (2 to n-1) 
    for index in range(1, N - 1):
  
        # Spaces for the diagonals 
        for side_index in range(2 * (N - index - 1)):
            print(' ', end = '')
  
        # Printing the diagonal values 
        print(Diagonal, end = '')
        Diagonal -= 1
        print()
  
    # Loop for printing the last row
    for index in range(N):
        print(Bottom, end = ' ')
        Bottom += 1
  
# Driver Code
  
# Number of rows
N = 5
alphabetPattern(N)
  
# This code is contributed 
# by SamyuktaSHegde


C#




// C# implementation of the approach
using System;
  
class GFG
{
// Function to print the desired
// Alphabet Z Pattern
static void alphabetPattern(int N)
{
  
    int index, side_index;
  
    // Declaring the values of Right,
    // Left and Diagonal values
    int Top = 1, Bottom = 1, Diagonal = N - 1;
  
    // Loop for printing the first row
    for (index = 0; index < N; index++)
        Console.Write(Top++ + " ");
  
    Console.WriteLine();
  
    // Main Loop for the rows from (2 to n-1)
    for (index = 1; index < N - 1; index++) 
    {
  
        // Spaces for the diagonals
        for (side_index = 0; side_index < 2 * (N - index - 1);
            side_index++)
            Console.Write(" ");
  
        // Printing the diagonal values
        Console.Write(Diagonal--);
  
        Console.WriteLine();
    }
  
    // Loop for printing the last row
    for (index = 0; index < N; index++)
        Console.Write(Bottom++ + " ");
}
  
// Driver Code
public static void Main()
{
    // Number of rows
    int N = 5;
  
    alphabetPattern(N);
}
}
  
// This code is contributed
// by Akanksha Rai


PHP




<?php
// PHP implementation of the approach
  
// Function to print the desired
// Alphabet Z Pattern
function alphabetPattern($N)
{
  
    $index; $side_index; $size;
  
    // Declaring the values of Right,
    // Left and Diagonal values
    $Top = 1; $Bottom = 1; $Diagonal = $N - 1;
  
    // Loop for printing the first row
    for ($index = 0; $index < $N; $index++)
        echo $Top++ . " ";
  
    echo "\n";
  
    // Main Loop for the rows from (2 to n-1)
    for ($index = 1; $index < $N - 1; $index++)
    {
  
        // Spaces for the diagonals
        for ($side_index = 0; 
             $side_index < 2 * ($N - $index - 1);
             $side_index++)
            echo " ";
  
        // Printing the diagonal values
        echo $Diagonal--;
  
        echo "\n";
    }
  
    // Loop for printing the last row
    for ($index = 0; $index < $N; $index++)
        echo $Bottom++ . " ";
}
  
// Driver Code
  
// Number of rows
$N = 5;
  
alphabetPattern($N);
  
// This code is contributed by Akanksha Rai


Javascript




<script>
  
      // JavaScript implementation of the approach
  
      // Function to print the desired
      // Alphabet Z Pattern
      function alphabetPattern(N) 
      {
        var index, side_index, size;
  
        // Declaring the values of Right,
        // Left and Diagonal values
        var Top = 1,
          Bottom = 1,
          Diagonal = N - 1;
  
        // Loop for printing the first row
        for (index = 0; index < N; index++) {
          document.write(Top + "  ");
          Top++;
        }
  
        document.write("<br>");
  
        // Main Loop for the rows from (2 to n-1)
        for (index = 1; index < N - 1; index++) {
          // Spaces for the diagonals
          for (side_index = 0; side_index < 
           2 * (N - index - 1); side_index++)
            document.write("  ");
  
          // Printing the diagonal values
          document.write(Diagonal);
          Diagonal--;
  
          document.write("<br>");
        }
  
        // Loop for printing the last row
        for (index = 0; index < N; index++) {
          document.write(Bottom + "  ");
          Bottom++;
        }
      }
  
      // Driver Code
      // Number of rows
      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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads