Q:

C# program to add two matrices

belongs to collection: C# Basic Programs | array programs

0

C# program to add two matrices

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 Matrix1Matrix2, and Matrix3 on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to transpose a matrix... >>
<< C# program to sort an integer array using the pred...