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 [Last updated : March 19, 2023]

Adding Two Matrices

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

C# code to add two matrices

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 »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.