Advertisement
viligen

diagonals_matrix

Jan 21st, 2022
843
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rows = int(input())
  2.  
  3. matrix = [[int(n) for n in input().split(', ')] for i in range(rows)]
  4.  
  5. primary_sum = 0
  6. primary = []
  7. secondary_sum = 0
  8. secondary = []
  9. for i in range(rows):
  10.     primary_sum += matrix[i][i]
  11.     primary.append(matrix[i][i])
  12.     for j in range(rows):
  13.         if i+j == rows - 1:
  14.             secondary_sum += matrix[i][j]
  15.             secondary.append(matrix[i][j])
  16.  
  17. print(f"Primary diagonal: {', '.join([str(n) for n in primary])}. Sum: {primary_sum}")
  18. print(f"Secondary diagonal: {', '.join([str(n) for n in secondary])}. Sum: {secondary_sum}")
  19.  
  20.  
  21.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement