Matrix Multiplication In C#

  1. using System;  
  2. class MatrixMultiplication {  
  3.     int[, ] a;  
  4.     int[, ] b;  
  5.     int[, ] c;  
  6.     public int m1, n1, m2, n2;  
  7.     public void ReadMatrix() {  
  8.         Console.WriteLine("\n Size of Matrix 1:");  
  9.         Console.Write("\n Enter the number of rows in Matrix 1 :");  
  10.         m1 = Convert.ToInt16(Console.ReadLine());  
  11.         Console.Write("\n Enter the number of columns in Matrix 1 :");  
  12.         n1 = Convert.ToInt16(Console.ReadLine());  
  13.         a = new int[m1, n1];  
  14.         Console.WriteLine("\n Size of Matrix 2 :");  
  15.         Console.Write("\n Enter the number of rows in Matrix 2 :");  
  16.         m2 = Convert.ToInt16(Console.ReadLine());  
  17.         Console.Write("\n Enter the number of columns in Matrix 2 :");  
  18.         n2 = Convert.ToInt16(Console.ReadLine());  
  19.         b = new int[m2, n2];  
  20.         if (n1 != m2) {  
  21.             Console.WriteLine("columns of A & Rows of B matrix are not equal");  
  22.             Console.ReadKey();  
  23.             Environment.Exit(0);  
  24.         } else {  
  25.             Console.WriteLine("\n Enter the elements of Matrix 1:");  
  26.             for (int i = 0; i < m1; i++) {  
  27.                 for (int j = 0; j < n1; j++) {  
  28.                     a[i, j] = Convert.ToInt16(Console.ReadLine());  
  29.                 }  
  30.             }  
  31.             Console.WriteLine("\n Enter the elements of Matrix 2:");  
  32.             for (int i = 0; i < m2; i++) {  
  33.                 for (int j = 0; j < n2; j++) {  
  34.                     b[i, j] = Convert.ToInt16(Console.ReadLine());  
  35.                 }  
  36.             }  
  37.         }  
  38.     }  
  39.     public void PrintMatrix() {  
  40.         Console.WriteLine("\n Matrix 1:");  
  41.         for (int i = 0; i < m1; i++) {  
  42.             for (int j = 0; j < n1; j++) {  
  43.                 Console.Write("\t" + a[i, j]);  
  44.             }  
  45.             Console.WriteLine();  
  46.         }  
  47.         Console.WriteLine("\n Matrix 2:");  
  48.         for (int i = 0; i < m2; i++) {  
  49.             for (int j = 0; j < n2; j++) {  
  50.                 Console.Write("\t" + b[i, j]);  
  51.             }  
  52.             Console.WriteLine();  
  53.         }  
  54.         Console.WriteLine("\n Resultant Matrix after multiplying:");  
  55.         for (int i = 0; i < m1; i++) {  
  56.             for (int j = 0; j < n2; j++) {  
  57.                 Console.Write("\t" + c[i, j]);  
  58.             }  
  59.             Console.WriteLine();  
  60.         }  
  61.         Console.ReadLine();  
  62.     }  
  63.     public void MultiplyMatrix() {  
  64.         c = new int[m1, n2];  
  65.         for (int i = 0; i < m1; i++) {  
  66.             for (int j = 0; j < n2; j++) {  
  67.                 c[i, j] = 0;  
  68.                 for (int k = 0; k < n1; k++) c[i, j] = c[i, j] + a[i, k] * b[k, j];  
  69.             }  
  70.         }  
  71.     }  
  72. }  
  73. class Matrices {  
  74.     public static void Main() {  
  75.         MatrixMultiplication MM = new MatrixMultiplication();  
  76.         MM.ReadMatrix();  
  77.         MM.MultiplyMatrix();  
  78.         MM.PrintMatrix();  
  79.     }  
  80. }