CSharp .Net Matrix Addtion Program with Output and Source code

C# .Net Matrix Addtion Program with Output and Source code

This c#.net program performs matrix addition. This matrix addition program with output and source code is shown below. Matrix addition program is compiled and executed using microsoft visual studio.

Matrix addition program Logic:


  • First we declare two integer variable row and columns to store the matrix dimensions entered by user
  • Declare three two dimensional arrays to store matrixA, matrixB and matrixC
  • Read matrix A using for loop and store it in array matrixA[row,columns]
  • Read matrix B using for loop and store it in array matrixB[row,columns]
  • Perform sum of matrix A and matrix B   using for loop and store it in array matrixC[row,columns] using matrixC[j, k] = matrixA[j, k] + matrixB[j, k];
  • show content of matrixA , matrixB and matrixC as sum of matrixA and matrixB

Matrix Addition Program Source Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrixAddition
{
    class MatrixAdditionProgram
    {
        static void Main(string[] args)
        {
            Console.Write("Enter No. of Rows :: ");
            int row = int.Parse(Console.ReadLine());

            Console.Write("Enter No. of Columns :: ");
            int column = int.Parse(Console.ReadLine());

            int[,] matrixA = new int[row, column];
            int[,] matrixB = new int[row, column];
            int[,] matrixC = new int[row, column];

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Enter Matrix B Data :: MatrixA [ " + row + " X " + column + "]");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write("Enter Data for [ " + j + "," + k + "]"+" ");
                    matrixA[j, k] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Enter Matrix B Data :: MatrixB [ " + row + " X " + column + "]");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write("Enter Data for [ " + j + "," + k + "]"+" ");
                    matrixB[j, k] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine(Environment.NewLine);
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    matrixC[j, k] = matrixA[j, k] + matrixB[j, k];
                }
            }

            Console.WriteLine("Matrix A is::");
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixA[j, k] + " ");
                }

                Console.WriteLine();
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Matrix B is::");
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixB[j, k] + " ");
                }

                Console.WriteLine();
            }
            Console.WriteLine(Environment.NewLine);


            Console.WriteLine("Sum of Matrix A and B is: ");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixC[j, k] + " ");
                }

                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

Matrix Addition Program Output

Enter No. of Rows :: 2
Enter No. of Columns :: 2

Enter Matrix B Data :: MatrixA [ 2 X 2]
Enter Data for [ 0,0] 2
Enter Data for [ 0,1] 2
Enter Data for [ 1,0] 2
Enter Data for [ 1,1] 2

Enter Matrix B Data :: MatrixB [ 2 X 2]
Enter Data for [ 0,0] 1
Enter Data for [ 0,1] 1
Enter Data for [ 1,0] 1
Enter Data for [ 1,1] 1

Matrix A is::
2 2
2 2

Matrix B is::
1 1
1 1

Sum of Matrix A and B is:
3 3
3 3

0 comments:

Post a Comment