generate functionFri, 10 Feb 2023

Write a program that contains the basic elements of a square matrix below the main diagonal. The elements of the main diagonal are also taken into account. The input is a natural number n is the number of rows and columns in the matrix, the number of matrix elements (integers) line by line, separated by a space.

n = int(input("Please insert n: ")) matrix = [[0 for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): matrix[i][j] = int(input("Please insert matrix[{}][{}]: ".format(i, j))) diagonal = 0 for i in range(n): for j in range(n): if i == j or i + j == n - 1: diagonal += matrix[i][j] print("The sum of the diagonal is: " + str(diagonal))

Questions about programming?Chat with your personal AI assistant