Print Z pattern in C

In this article, we will discuss the C program to print the Z pattern in C. Given a number, such as 1, 2, 3, print the alphabetic Z Pattern as shown below:

Example:

Input = 6

Input = 7

Algorithm

  • Print the first row of numbers from 1 to Num.
  • After that, from the second to the (Num-1)th row, print 2 * (Num- in- 1) blank spaces followed by the concluding element, in- 1.
  • Display the last row with numbers ranging from 1 to Num.

Program to print z pattern:

Output:

1 2 3 4 5 6 
         5
      4
    3
  2
1 2 3 4 5 6 

Explanation:

  • In this program, the alphabetic Z pattern is printed with this function. It accepts one input Num, the size of the Z pattern to be produced.
  • Variables:
  • This variable acts like a loop counter during multiple loops.
  • side_indexes: This variable specifies the number of spaces to be printed on either side of the diagonal.
  • sizes: These are not used in the program.
  • Tops: This variable represents the values displayed in the Z pattern's top row.
  • down: It indicates what values will be printed in the Z pattern's bottom row.
  • Diag: The diagonal values are represented by this variable.
  • It displays the first row of integers ranging from 1 to Num(in this example, 1 to 6).
  • After that, the diagonal element of the Z pattern is printed, with gaps on both sides and decreasing numerals from Num-1 to 1.
  • Finally, it displays the bottom row containing numbers ranging from 1 to Num.