Print Z Pattern In C

Input = 6

Example

1 2 3 4 5 6 

          5

        4

     3

  2

1 2 3 4 5 6

Input = 7

Example

1 2 3 4 5 6 7 

           6

        5

      4

    3

  2

1 2 3 4 5 6 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:

    Example
    
    // implementation of Z pattern
    
    #include <stdio.h>
    
    // Programme to print the required 
    
    // Alphabetic Z Pattern
    
    void alphabetZpattern(int Num)
    
    {
    
    	int in, side_indexs, sizes;
    
    
    
    	// Identifying the values for Right, 
    
    	// Left, as well as Diagonal
    
    	int Tops = 1, down = 1, Diag = Num - 1;
    
    	// Loop for printing the first row
    
    	for (in = 0; in < Num; in++)
    
    		printf("%d ", Tops++);
    
    	printf("\n");
    
    	// loop iterating over the indexes
    
    	for (in= 1; in< Num - 1; in++) {
    
    		// loop iteration
    
    		for (side_indexs = 0; side_indexs < 2 * (Num - in - 1);
    
    			side_indexs++)
    
    			printf(" ");
    
    		// displaying the diagonal values
    
    		printf("%d", Diag--);
    
    		printf("\n");
    
    	}
    
    	// loop iterating over the last row
    
    	for (in= 0; in < Num; in++)
    
    		printf("%d ", down++);
    
    }
    
    
    
    // Driver Code
    
    int main()
    
    {
    
    	// initialize the n with size 
    
    	int N = 6;
    
    	alphabetZpattern(N);
    
    	return 0;
    
    }
    

Output:

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 .

Input Required

This code uses input(). Please provide values below: