
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to multiply two matrices
The program for matrix multiplication is used to multiply two matrices. This procedure is only possible if the number of columns in the first matrix are equal to the number of rows in the second matrix.
A program that demonstrates matrix multiplication in C# is given as follows −
Example
using System; namespace MatrixMultiplicationDemo { class Example { static void Main(string[] args) { int m = 2, n = 3, p = 3, q = 3, i, j; int[,] a = {{1, 4, 2}, {2, 5, 1}}; int[,] b = {{3, 4, 2}, {3, 5, 7}, {1, 2, 1}}; Console.WriteLine("Matrix a:"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { Console.Write(a[i, j] + " "); } Console.WriteLine(); } Console.WriteLine("Matrix b:"); for (i = 0; i < p; i++) { for (j = 0; j < q; j++) { Console.Write(b[i, j] + " "); } Console.WriteLine(); } if(n! = p) { Console.WriteLine("Matrix multiplication not possible"); } else { int[,] c = new int[m, q]; for (i = 0; i < m; i++) { for (j = 0; j < q; j++) { c[i, j] = 0; for (int k = 0; k < n; k++) { c[i, j] += a[i, k] * b[k, j]; } } } Console.WriteLine("The product of the two matrices is :"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { Console.Write(c[i, j] + "\t"); } Console.WriteLine(); } } } } }
Output
The output of the above program is given as follows.
Matrix a: 1 4 2 2 5 1 Matrix b: 3 4 2 3 5 7 1 2 1 The product of the two matrices is : 172832 223540
Now let us understand the above program.
First, the two matrices a and b are displayed. The code snippet for this is given as follows.
for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { Console.Write(a[i, j] + " "); } Console.WriteLine(); } Console.WriteLine("Matrix b:"); for (i = 0; i < p; i++) { for (j = 0; j < q; j++) { Console.Write(b[i, j] + " "); } Console.WriteLine(); }
If the number of columns in the first matrix are not equal to the number of rows in the second matrix then the matrices cannot be multiplied and this is displayed. The code snippet for this is given as follows .
if(n! = p) { Console.WriteLine("Matrix multiplication not possible"); }
Otherwise, a nested for loop is used to obtain the product of matrices a and b i.e. matrix c. Then the matrix c is displayed. The code snippet for this is given as follows −
for (i = 0; i < m; i++) { for (j = 0; j < q; j++) { c[i, j] = 0; for (int k = 0; k < n; k++) { c[i, j] += a[i, k] * b[k, j]; } } } Console.WriteLine("The product of the two matrices is :"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { Console.Write(c[i, j] + "\t"); } Console.WriteLine(); }
- Related Articles
- Java program to multiply two matrices.
- Python program to multiply two matrices
- Program to multiply two matrices in C++
- Swift Program to Multiply two Matrices Using Multi-dimensional Arrays
- C++ Program to Multiply two Matrices by Passing Matrix to Function
- How to Multiply Two Matrices using Python?
- Golang Program to Multiply two Matrices by Passing Matrix to a Function
- Swift Program to Multiply two Matrices by Passing Matrix to a Function
- How to multiply two matrices by elements in R?
- How to multiply two matrices using pointers in C?
- How to multiply corresponding values from two matrices in R?
- How can Tensorflow be used to multiply two matrices using Python?
- Java program to add two matrices.
- Java program to subtract two matrices.
- C# program to add two matrices
