Monday 10 December 2012

Code for Multiplication Matrix

using System;
namespace Console_MatrixMulti
{
    class MatrixMultiplication
    {
        public void MM()
        {
            //Declaring array to accept input from users
            int i, j;
            Console.WriteLine("Enter the rows :");
            int m = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the Column :");
            int n = int.Parse(Console.ReadLine());

            int[,] a = new int[m, n];
            int[,] b = new int[m, n];
            int[,] multi = new int[m, n];

            //Input 1st Matrix
            Console.WriteLine("Enter the 1st matrix :");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    a[i, j] = int.Parse(Console.ReadLine());
                }
            }

            //Input 2nd Matrix
            Console.WriteLine("Enter the 2nd matrix :");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    b[i, j] = int.Parse(Console.ReadLine());

                }
            }

            //Sum operation on 1st and 2nd Matrix
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    multi[i, j] = a[i, j] * b[i, j];
                }
            }

            //for print the both matrix
            Console.WriteLine("Muliplication of both matrix is :");
            for (i = 0; i < m; i++)
            {
                Console.WriteLine("");
                for (j = 0; j < n; j++)
                {
                    Console.Write(multi[i, j]);
                    Console.Write("\t");
                }
               
            }
        }
        static void Main(string[] args)
        {
            MatrixMultiplication obj = new MatrixMultiplication();
            obj.MM();
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment