- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- string rows_a;
- string columns_a;
- string rows_b;
- string columns_b;
- string m;
- string n;
- int[,] a = new int[4, 4];
- int[,] b = new int[4, 4];
- int[,] c = new int[4, 4];
- int i = 0;
- int j = 0;
- int k = 0;
- int l = 0;
- c[i, j] = 0;
- int no_of_rows_a = 0;
- int no_of_columns_a = 0;
- int no_of_rows_b = 0;
- int no_of_columns_b = 0;
- Console.WriteLine(" Enter the number of rows for matrix A");
- rows_a = Console.ReadLine();
- no_of_rows_a = Convert.ToInt32(rows_a);
- Console.WriteLine(" Enter the number of columns for matrix A");
- columns_a = Console.ReadLine();
- no_of_columns_a = Convert.ToInt32(columns_a);
- Console.WriteLine(" Enter the number of rows for matrix B");
- rows_b = Console.ReadLine();
- no_of_rows_b = Convert.ToInt32(rows_b);
- Console.WriteLine(" Enter the number of columns for matrix B");
- columns_b = Console.ReadLine();
- no_of_columns_b = Convert.ToInt32(columns_b);
- Console.WriteLine(" The values of A matrix is :");
- for (i = 0; i < no_of_rows_a; i++)
- {
- for (j = 0; j < no_of_columns_a; j++)
- {
- m = Console.ReadLine();
- k = Convert.ToInt32(m);
- a[i, j] = k;
- }
- }
- Console.WriteLine(" The values of B matrix is :");
- for (i = 0; i < no_of_rows_b; i++)
- {
- for (j = 0; j < no_of_columns_b; j++)
- {
- n = Console.ReadLine();
- l = Convert.ToInt32(n);
- b[i, j] = l;
- }
- }
- Console.WriteLine(" The matrix format of A is ");
- for (i = 0; i < no_of_rows_a; i++)
- {
- for (j = 0; j < no_of_columns_a; j++)
- {
- Console.Write(" {0}", a[i, j]);
- }
- Console.WriteLine("");
- }
- Console.WriteLine(" The matrix format of B is ");
- for (i = 0; i < no_of_rows_b; i++)
- {
- for (j = 0; j < no_of_columns_b; j++)
- {
- Console.Write(" {0}",b[i,j]);
- }
- Console.WriteLine("");
- }
- if (columns_a == rows_b)
- {
- for (i = 0; i < no_of_rows_a; i++)
- {
- for (j = 0; j < no_of_columns_b; j++)
- {
- c[i, j] = (a[i, 0] * b[0, j]) + (a[i, 1] * b[1, j]);
- Console.Write(" ");
- }
- Console.WriteLine("");
- }
- }
- else
- {
- Console.WriteLine("The matrix multiplication cannot be performed: Logic violation");
- }
- Console.WriteLine(" The resultant matrix is");
- for (i = 0; i < no_of_rows_a; i++)
- {
- for (j = 0; j < no_of_columns_b; j++)
- {
- Console.Write(" {0}", c[i, j]);
- }
- Console.WriteLine();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- }
- }