Select Page

# Program to print the matrix in Z form using Python

by | Aug 12, 2021 | Python Programs

Home » Python » Python Programs » Program to print the matrix in Z form using Python

## Introduction

Given a matrix of size n*n, the task is to print the elements in Z form. ## Program

```ip_array = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

length = len(ip_array)

#Print first line
n = 0
for m in range(0, length):
print(ip_array[n][m])
#Print diagonals
l = 1
for n in range(0, length):
for j in range(length-2, 0, -1):
if(j == length - l):
print(ip_array[n][j])
break;
l += 1
#Print last line
n = length - 1;
for j in range(0, length):
print(ip_array[n][j])```

## Output ## Explanation

In the above code our approach is:

• First traversed and printed all the elements at position a[m] where m in range (0, length).
• Traverse and print all the diagonal elements at a[n][j] where n in range(0,length) and j in range(length-2, 0, -1).
• At last traverse and print all the elements at position a[n][j] where n = length-1 and j in range(0, length)

## Author

• A Full Stack Developer with 10+ years of experience in technical content creation.

• 