Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Matrix multiplication in C#

Kindly remember the multiplication logic for matrices.
The no of columns of first matrix must be equal to no of rows of second matrix.
However, it is handled in the program code.

Please press 'enter' button after entering each value.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication2
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. try
  13.  
  14. {
  15. string rows_a;
  16.  
  17. string columns_a;
  18.  
  19. string rows_b;
  20.  
  21. string columns_b;
  22.  
  23. string m;
  24.  
  25. string n;
  26.  
  27. int[,] a = new int[4, 4];
  28.  
  29. int[,] b = new int[4, 4];
  30.  
  31. int[,] c = new int[4, 4];
  32.  
  33.  
  34. int i = 0;
  35.  
  36. int j = 0;
  37.  
  38. int k = 0;
  39.  
  40. int l = 0;
  41.  
  42. c[i, j] = 0;
  43.  
  44. int no_of_rows_a = 0;
  45.  
  46. int no_of_columns_a = 0;
  47.  
  48. int no_of_rows_b = 0;
  49.  
  50. int no_of_columns_b = 0;
  51.  
  52. Console.WriteLine(" Enter the number of rows for matrix A");
  53.  
  54. rows_a = Console.ReadLine();
  55.  
  56. no_of_rows_a = Convert.ToInt32(rows_a);
  57.  
  58. Console.WriteLine(" Enter the number of columns for matrix A");
  59.  
  60. columns_a = Console.ReadLine();
  61.  
  62. no_of_columns_a = Convert.ToInt32(columns_a);
  63.  
  64. Console.WriteLine(" Enter the number of rows for matrix B");
  65.  
  66. rows_b = Console.ReadLine();
  67.  
  68. no_of_rows_b = Convert.ToInt32(rows_b);
  69.  
  70. Console.WriteLine(" Enter the number of columns for matrix B");
  71.  
  72. columns_b = Console.ReadLine();
  73.  
  74. no_of_columns_b = Convert.ToInt32(columns_b);
  75.  
  76. Console.WriteLine(" The values of A matrix is :");
  77.  
  78. for (i = 0; i < no_of_rows_a; i++)
  79.  
  80. {
  81.  
  82. for (j = 0; j < no_of_columns_a; j++)
  83.  
  84. {
  85.  
  86. m = Console.ReadLine();
  87.  
  88. k = Convert.ToInt32(m);
  89.  
  90. a[i, j] = k;
  91.  
  92. }
  93.  
  94. }
  95.  
  96. Console.WriteLine(" The values of B matrix is :");
  97.  
  98. for (i = 0; i < no_of_rows_b; i++)
  99.  
  100. {
  101.  
  102. for (j = 0; j < no_of_columns_b; j++)
  103.  
  104. {
  105.  
  106. n = Console.ReadLine();
  107.  
  108. l = Convert.ToInt32(n);
  109.  
  110. b[i, j] = l;
  111.  
  112. }
  113.  
  114. }
  115.  
  116.  
  117. Console.WriteLine(" The matrix format of A is ");
  118.  
  119.  
  120. for (i = 0; i < no_of_rows_a; i++)
  121.  
  122. {
  123.  
  124. for (j = 0; j < no_of_columns_a; j++)
  125.  
  126. {
  127.  
  128. Console.Write(" {0}", a[i, j]);
  129.  
  130. }
  131.  
  132. Console.WriteLine("");
  133.  
  134. }
  135.  
  136. Console.WriteLine(" The matrix format of B is ");
  137.  
  138. for (i = 0; i < no_of_rows_b; i++)
  139.  
  140. {
  141.  
  142. for (j = 0; j < no_of_columns_b; j++)
  143.  
  144. {
  145.  
  146. Console.Write(" {0}",b[i,j]);
  147.  
  148. }
  149.  
  150. Console.WriteLine("");
  151.  
  152. }
  153.  
  154. if (columns_a == rows_b)
  155.  
  156. {
  157.  
  158. for (i = 0; i < no_of_rows_a; i++)
  159.  
  160. {
  161.  
  162. for (j = 0; j < no_of_columns_b; j++)
  163.  
  164. {
  165.  
  166. c[i, j] = (a[i, 0] * b[0, j]) + (a[i, 1] * b[1, j]);
  167.  
  168. Console.Write(" ");
  169.  
  170. }
  171.  
  172. Console.WriteLine("");
  173.  
  174. }
  175.  
  176. }
  177.  
  178. else
  179.  
  180. {
  181.  
  182. Console.WriteLine("The matrix multiplication cannot be performed: Logic violation");
  183.  
  184.  
  185. }
  186.  
  187. Console.WriteLine(" The resultant matrix is");
  188.  
  189. for (i = 0; i < no_of_rows_a; i++)
  190.  
  191. {
  192.  
  193. for (j = 0; j < no_of_columns_b; j++)
  194.  
  195. {
  196.  
  197. Console.Write(" {0}", c[i, j]);
  198.  
  199. }
  200.  
  201. Console.WriteLine();
  202.  
  203. }
  204.  
  205. }
  206.  
  207. catch (Exception e)
  208. {
  209.  
  210. Console.WriteLine(e.Message);
  211.  
  212. }
  213. }
  214. }
  215. }
  216.