CSharp Net Matrix Substraction Program with output

CSharp Matrix Substraction Program Using arrays with output

This C#.net program is about Matrix Substraction with output and source code.This matrix substraction program with output and source code is shown below. Matrix substraction program is compiled and executed using microsoft visual studio.

Matrix substraction 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 substraction of matrixA and matrixB

C# Matrix Substraction Program Code

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

namespace MatrixSubstraction
{
    class Program
    {
        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("Substraction 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();
        }
    }
}

C# Matrix Substraction 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

Substraction of Matrix A and B is:
1 1
1 1

0 comments:

Post a Comment