C# Program to Subtract Two Matrices

This is a C# Program to perform matrix subtraction.

Problem Description

This C# Program Performs Matrix Subtraction.

Problem Solution

The dimensions of the two matrices are obtained from the user and its subtraction is performed by subtracting corresponding elements.

Program/Source Code

Here is source code of the C# program to perform Matrix Subtraction. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform Matrix Subtraction
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class Program
    {
        public static void Main(string[] args)
        {
            int m, n, i, j;
            Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : ");
            m = Convert.ToInt16(Console.ReadLine());
            n = Convert.ToInt16(Console.ReadLine());
            int[,] A = new int[10, 10];
            Console.Write("\nEnter The First Matrix : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    A[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }
 
            int[,] B = new int[10, 10];
            Console.Write("\nEnter The Second Matrix:");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    B[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }
            Console.Clear();
            Console.WriteLine("\nMatrix A : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(A[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
            Console.WriteLine("\nMatrix B: ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(B[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
            int[,] C = new int[10, 10];
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    C[i, j] = A[i, j] - B[i, j];
                }
            }
            Console.Write("\nDifference Matrix :");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(C[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}
Program Explanation

This c# program, we are reading the number of rows and columns of matrices ‘a’ and ‘b’ using ‘m’ and ‘n’ variables respectively. Using for loop we are entering the coefficient element values for both the matrix a and b using a[i,j] and b[i,j] array variables respectively. Compute the difference between each element in both the matrix ‘a’ and ‘b’ respectively.

advertisement
advertisement
Runtime Test Cases
 
Enter Number Of Rows And Columns Of Matrices A and B : 3  3
Enter the First Matrix : 
9 8 7
6 5 4
7 8 9
Enter the Second Matrix :
6 5 5
3 4 2
1 2 3
Matrix A :
9 8 7
6 5 4
7 8 9
Matrix B :
6 5 5
3 4 2
1 2 3
Difference Matrix :
3 3 2
3 1 2
6 6 6

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.