Python Online Compiler
Example: Program to Find Normal and Trace of a Matrix in Python using For loop
C
C++
C#
Java
Python
PHP
main.py
STDIN
Run
# Program to Find Normal and Trace of a Matrix in Python using For loop import math # to use the math.sqrt function i, j, n, sum, sum1, a, normal, x = 0, 0, 0, 0, 0, 0, 0, [] print ("Enter the number of rows[columns] of the matrix::") n = int (input ()) print ("\n----Enter the ", n * n, " elements of the first matrix----\n") for i in range (n): x.append([]) for j in range (n): x[i].append(int (input ())) a = x[i][j] * x[i][j] sum1 += a normal = math.sqrt(sum1) print ("\n\nThe normal of the given matrix is: ", normal) for i in range (n): sum = sum + x[i][i] print ("\n\nThe Trace of the given matrix is: ", sum)
3 43 54 4 3 54 23 54 23 5
Output
Clear
ADVERTISEMENTS