C# program to add two matrices

Here, we are going to learn how to add two matrices in C#?
Submitted by Nidhi, on November 02, 2020

Here, we will read two matrixes and then add store the addition of matrixes into the third matrix.

Program:

The source code to add two matrices is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to add two matrices.

using System;

class MatrixDemo
{
    public static void Main(string[] args)
    {
        int i = 0;
        int j = 0;

        int row = 2;
        int col = 2;

        int[,] Matrix1 = new int[row, col];
        int[,] Matrix2 = new int[row, col];
        int[,] Matrix3 = new int[row, col];

        Console.WriteLine("Enter the elements of Matrix1: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Matrix1[i, j] = int.Parse(Console.ReadLine());
            }
        }

        Console.WriteLine("Enter the elements of Matrix2: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Matrix2[i, j] = int.Parse(Console.ReadLine());
            }
        }

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

            }
            Console.WriteLine();
        }

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

            }
            Console.WriteLine();
        }
        
        Console.WriteLine("\nAddition of Matrix1 and Matrix2:");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Console.Write(Matrix3[i, j] + "\t");

            }
            Console.WriteLine();
        }
    }
}

Output:

Enter the elements of Matrix1:
1
2
3
4
Enter the elements of Matrix2:
5
6
7
8

Matrix1:
1       2
3       4

Matrix2:
5       6
7       8

Addition of Matrix1 and Matrix2:
6       8
10      12
Press any key to continue . . .

Explanation:

In the above program, we created a class MatrixDemo that contains a Main() method. The Main() method is the entry point for the program, Here, we created three 2-D arrays to represent a matrix with a size of 2X2. Then read the elements for Matrix1 and Matrix2.

for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Matrix3[i, j] = Matrix1[i, j] + Matrix2[i, j];
    }
}

Using the above code, we add Matrix1 and Matrix2 and assign the addition of both into Matrix3. Then we print the elements of Matrix1, Matrix2, and Matrix3 on the console screen.

C# Basic Programs »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.