w3resource

C#: Subtraction of two Matrices

C# Sharp Array: Exercise-20 with Solution

Write a C# Sharp program for the subtraction of two matrixes.

Sample Solution:-

C# Sharp Code:

using System;

public class Exercise20  
{  
    public static void Main() 
    {
        int i, j, n; // Declare variables for loop control and matrix size
        int[,] arr1 = new int[50, 50]; // Declare the first matrix
        int[,] brr1 = new int[50, 50]; // Declare the second matrix
        int[,] crr1 = new int[50, 50]; // Declare the resultant matrix for subtraction

        // Display a message prompting the user to input the size of the square matrix (less than 5)
        Console.Write("\n\nSubtraction of two Matrices :\n");
        Console.Write("------------------------------\n");  
        Console.Write("Input the size of the square matrix (less than 5): ");
        n = Convert.ToInt32(Console.ReadLine());

        /* Input elements into the first matrix */
        Console.Write("Input elements in the first matrix :\n");
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                Console.Write("element - [{0},{1}] : ", i, j);
                arr1[i, j] = Convert.ToInt32(Console.ReadLine()); // Store user input in the first matrix
            }
        }   

        /* Input elements into the second matrix */
        Console.Write("Input elements in the second matrix :\n");
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                Console.Write("element - [{0},{1}] : ", i, j);
                brr1[i, j] = Convert.ToInt32(Console.ReadLine()); // Store user input in the second matrix
            }
        }    

        // Display the first matrix
        Console.Write("\nThe First matrix is :\n");
        for (i = 0; i < n; i++)
        {
            Console.Write("\n");
            for (j = 0; j < n; j++)
                Console.Write("{0}\t", arr1[i, j]); // Print elements of the first matrix
        }

        // Display the second matrix
        Console.Write("\nThe Second matrix is :\n");
        for (i = 0; i < n; i++)
        {
            Console.Write("\n");
            for (j = 0; j < n; j++)
                Console.Write("{0}\t", brr1[i, j]); // Print elements of the second matrix
        }

        /* Calculate the subtraction of the matrices */
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
                crr1[i, j] = arr1[i, j] - brr1[i, j]; // Perform subtraction of elements and store in the resultant matrix
        }

        // Display the resultant matrix after subtraction
        Console.Write("\nThe Subtraction of two matrices is : \n");
        for (i = 0; i < n; i++)
        {
            Console.Write("\n");
            for (j = 0; j < n; j++)
                Console.Write("{0}\t", crr1[i, j]); // Print elements of the resultant matrix
        }
        Console.Write("\n\n");
    }
}

Sample Output:

Subtraction of two Matrices :                                                                                 
------------------------------                                                                                
Input the size of the square matrix (less than 5): 2                                                          
Input elements in the first matrix :                                                                          
element - [0,0] : 2                                                                                           
element - [0,1] : 4                                                                                           
element - [1,0] : 6                                                                                           
element - [1,1] : 8                                                                                           
Input elements in the second matrix :                                                                         
element - [0,0] : 1                                                                                           
element - [0,1] : 3                                                                                           
element - [1,0] : 5                                                                                           
element - [1,1] : 7                                                                                           
The First matrix is :                                                                                         
2       4                                                                                                     
6       8                                                                                                     
The Second matrix is :
1       3                                                                                                     
5       7                                                                                                     
The Subtraction of two matrix is :                                                                            
1       1                                                                                                     
1       1

Flowchart:


C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp for addition of two Matrices of same size.
Next: Write a program in C# Sharp for multiplication of two square Matrices.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.