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++
// 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; } |
chevron_right
filter_none
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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 = 5alphabetPattern(N) # This code is contributed # by SamyuktaSHegde |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
Output:
1 2 3 4 5
4
3
2
1 2 3 4 5
Recommended Posts:
- Program to print the given H Pattern
- Program to print the pattern "GFG"
- Program to print the pattern ‘D’
- Program to print the pattern 'G'
- Program to print pattern
- Program to print Crown Pattern
- Program to print a pattern of numbers
- Program to print pyramid pattern
- Program to print a rectangle pattern
- Program to Print the Trapezium Pattern
- Program to print pyramid pattern
- Program to print circle pattern
- Program to print Step Pattern
- Program to print numeric pattern
- Program to print numeric pattern | Set - 2
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.



