1+ print ("🎮 Matrix Calculator 🎮" )
2+ print ("Easily add, subtract, multiply, or transpose matrices! \n " )
3+
4+ while True :
5+ choice = input ("🎯 Choose Operation: Add (A), Subtract (S), Multiply (M), Transpose (T), or Quit (Q): " ).upper ()
6+
7+ if choice in ["Q" , "QUIT" ]:
8+ break
9+
10+ elif choice in ["A" , "S" ]:
11+ try :
12+ rows = int (input ("➡️ Enter number of rows: " ))
13+ cols = int (input ("➡️ Enter number of columns: " ))
14+
15+ print (f"\n 📝 Enter elements for Matrix 1 ({ rows } x{ cols } ) row by row (space separated):" )
16+ matrix1 = []
17+ for _ in range (rows ):
18+ row = list (map (float , input ("🔢 " ).split ()))
19+ matrix1 .append (row )
20+
21+ print (f"\n 📝 Enter elements for Matrix 2 ({ rows } x{ cols } ) row by row (space separated):" )
22+ matrix2 = []
23+ for _ in range (rows ):
24+ row = list (map (float , input ("🔢 " ).split ()))
25+ matrix2 .append (row )
26+
27+ print ("\n 📊 Resulting Matrix:" )
28+ for i in range (rows ):
29+ result_row = []
30+ for j in range (cols ):
31+ if choice == "A" :
32+ val = matrix1 [i ][j ] + matrix2 [i ][j ]
33+ else :
34+ val = matrix1 [i ][j ] - matrix2 [i ][j ]
35+ result_row .append (str (val ))
36+ print ("\t " .join (result_row ))
37+
38+ print ("✅ Calculation successful!\n " )
39+
40+ except Exception :
41+ print ("❌ Error: Please enter valid numbers or ensure dimensions match.\n " )
42+
43+ elif choice == "M" :
44+ try :
45+ r1 = int (input ("➡️ Enter number of rows for Matrix 1: " ))
46+ c1 = int (input ("➡️ Enter number of columns for Matrix 1: " ))
47+
48+ print (f"\n 📝 Enter elements for Matrix 1 ({ r1 } x{ c1 } ) row by row (space separated):" )
49+ matrix1 = []
50+ for _ in range (r1 ):
51+ row = list (map (float , input ("🔢 " ).split ()))
52+ matrix1 .append (row )
53+
54+ r2 = int (input (f"\n ➡️ Enter number of rows for Matrix 2 (MUST be { c1 } ): " ))
55+ c2 = int (input ("➡️ Enter number of columns for Matrix 2: " ))
56+
57+ if c1 != r2 :
58+ print ("❌ Error: Matrix 1 columns must equal Matrix 2 rows for multiplication.\n " )
59+ continue
60+
61+ print (f"\n 📝 Enter elements for Matrix 2 ({ r2 } x{ c2 } ) row by row (space separated):" )
62+ matrix2 = []
63+ for _ in range (r2 ):
64+ row = list (map (float , input ("🔢 " ).split ()))
65+ matrix2 .append (row )
66+
67+ print ("\n 📊 Resulting Matrix:" )
68+ for i in range (r1 ):
69+ result_row = []
70+ for j in range (c2 ):
71+ val = 0
72+ for k in range (c1 ):
73+ val += matrix1 [i ][k ] * matrix2 [k ][j ]
74+ result_row .append (str (val ))
75+ print ("\t " .join (result_row ))
76+ print ("✅ Calculation successful!\n " )
77+
78+ except Exception :
79+ print ("❌ Error: Please enter valid numbers or ensure dimensions match.\n " )
80+
81+ elif choice == "T" :
82+ try :
83+ r = int (input ("➡️ Enter number of rows: " ))
84+ c = int (input ("➡️ Enter number of columns: " ))
85+
86+ print (f"\n 📝 Enter elements for Matrix ({ r } x{ c } ) row by row (space separated):" )
87+ matrix = []
88+ for _ in range (r ):
89+ row = list (map (float , input ("🔢 " ).split ()))
90+ matrix .append (row )
91+
92+ print ("\n 📊 Transposed Matrix:" )
93+ for j in range (c ):
94+ result_row = []
95+ for i in range (r ):
96+ result_row .append (str (matrix [i ][j ]))
97+ print ("\t " .join (result_row ))
98+ print ("✅ Calculation successful!\n " )
99+
100+ except Exception :
101+ print ("❌ Error: Please enter valid numbers.\n " )
102+
103+ else :
104+ print ("⚠️ Invalid input\n " )
105+
106+ print ("\n 👋 Thanks for using Matrix Calculator! Goodbye!\n " )
0 commit comments