-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.py
More file actions
29 lines (25 loc) · 831 Bytes
/
matrix.py
File metadata and controls
29 lines (25 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Function to print a matrix
def print_matrix(matrix):
for row in matrix:
for element in row:
print(element, end=' ')
print() # for new line after each row
# Function to create a matrix from user input
def input_matrix(rows, cols):
matrix = []
print("Enter the elements row by row:")
for i in range(rows):
row = []
for j in range(cols):
element = int(input(f"Element [{i+1}, {j+1}]: "))
row.append(element)
matrix.append(row)
return matrix
# Get user input for the number of rows and columns
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))
# Get user input for the matrix elements
matrix = input_matrix(rows, cols)
# Print the matrix
print("The matrix is:")
print_matrix(matrix)