Saturday 25 April 2015

Addition of two matrix in c# asp.net.


In the program below I have written the addition of two matrix in c#. This program will ask to enter the number of row and column of both matrix. Then it will ask enter all elements of both matrix one by one. And  then it will print the both matrx and thrid  matrxi as a sum of both.

Source code:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;

namespace ConsoleDemoApp
{
    class MatrxAddition
    {
        /* program to write a exception in c# */

        public static void Main(string[] args)
        {
            int row, col, i, j;
            Console.Write("Enter Number Of Rows And Columns Of Matrices A : ");
            row = Convert.ToInt16(Console.ReadLine());
            Console.Write("Enter Number Of Rows And Columns Of Matrices B : ");
            col = Convert.ToInt16(Console.ReadLine());
            int[,] A = new int[row, col];
            Console.Write("\nEnter The First Matrix : \n");
            for (i = 0; i < row; i++)
            {
                for (j = 0; j <  col; j++)
                {
                    A[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }

            int[,] B = new int[row, col];
            Console.Write("\nEnter The Second Matrix:\n");
            for (i = 0; i < row; i++)
            {
                for (j = 0; j <  col; j++)
                {
                    B[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }
            //Console.Clear();
            Console.WriteLine("\nMatrix A : ");
            for (i = 0; i < row; i++)
            {
                for (j = 0; j <  col; j++)
                {
                    Console.Write(A[i, j] + "\t");

                }
                Console.WriteLine();
            }
            Console.WriteLine("\nMatrix B: ");
            for (i = 0; i < row; i++)
            {
                for (j = 0; j <  col; j++)
                {
                    Console.Write(B[i, j] + "\t");

                }
                Console.WriteLine();
            }
            int[,] C = new int[10, 10];
            for (i = 0; i < row; i++)
            {
                for (j = 0; j <  col; j++)
                {
                    C[i, j] = A[i, j] + B[i, j];
                }
            }
            Console.Write("\nSum Matrix :\n");
            for (i = 0; i < row; i++)
            {
                for (j = 0; j <  col; j++)
                {
                    Console.Write(C[i, j] + "\t");

                }
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}


Output



No comments:

Post a Comment