Matrix Diagonal Pattern

 Coding Problem Keys 

 Matrix Diagonal Pattern 

 Problem Statement 

Given an integer matrix of size N*N as input, the program must print only the diagonal elements. The remaining elements must be replaced by *.

 Boundary Condition(s) 

1 <= N <= 50

 Input Format 

The first line contains the value of N.
The next N lines contain N values separated by space(s).

 Output Format 

The first N lines contain the modified matrix as mentioned above.

 Example Input/Output 1 

 Input 

3
1 8 9
4 8 7
5 2 8

 Output 

1 * 9
* 8 *
5 * 8

 Example Input/Output 2 

 Input 

6
31 62 5 8 39 3
37 2 79 76 97 81
9 1 43 86 5 37
9 77 85 45 87 36
41 6 65 75 9 3
22 69 45 83 20 90

 Output 

31 * * * * 3
* 2 * * 97 *
* * 43 86 * *
* * 85 45 * *
* 6 * * 9 *
22 * * * * 90

 Max Execution Time Limit: 5000 millisecs 

 Solution 

 Programming Language: Python 3 Language 

n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    for j in range(n):
        if i==j or i+j==n-1:
            print(l[i][j],end=" ")
        else:
            print('*',end=" ")
    print()

# Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Unique Characters

Difference between a number and it's reverse

String - Special Characters

Reverse Array

Find Co-ordinates - Other Side

Vowels Count