
- 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
How to perform Matrix Addition using C#?
To perform matrix addition, take two matrices. Enter the rows and columns of matrix one and matrix two. Remember, both the matrix should be a square matrix to add them.
Now add elements to both the matrices. Declare a new array and add both the arrays in it.
arr3[i, j] = arr1[i, j] + arr2[i, j];
Let us see the complete code −
Example
using System; using System.Linq; class Demo { static void Main() { int m, n, i, j; Console.Write("Enter number of rows and columns of the matrix "); m = Convert.ToInt16(Console.ReadLine()); n = Convert.ToInt16(Console.ReadLine()); int[,] arr1 = new int[10, 10]; int[,] arr2 = new int[10, 10]; int[,] arr3 = new int[10, 10]; Console.Write("Enter elements - Matrix 1 : "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { arr1[i, j] = Convert.ToInt16(Console.ReadLine()); } } Console.Write("Enter elements - Matrix 2 : "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { arr2[i, j] = Convert.ToInt16(Console.ReadLine()); } } Console.WriteLine("Matrix 1 "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { Console.Write(arr1[i, j] + "\t"); } Console.WriteLine(); } Console.WriteLine("Matrix 2 "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { Console.Write(arr2[i, j] + "\t"); } Console.WriteLine(); } for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { arr3[i, j] = arr1[i, j] + arr2[i, j]; } Console.WriteLine(); } Console.WriteLine("Matrix Addition "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { Console.Write(arr3[i, j] + "\t"); } Console.WriteLine(); } Console.ReadLine(); } }
Output
The following is the output.
Enter number of rows and columns of the matrix 3 3 Enter elements - Matrix 1 : 1 2 3 4 5 6 7 8 9 Enter elements - Matrix 2 : 1 2 3 4 5 6 7 8 9 Matrix 1 123 456 789 Matrix 2 123 456 789 Matrix Addition 246 81012 141618
- Related Articles
- C++ Program to Perform Addition Operation Using Bitwise Operators
- Addition and Subtraction of Matrix using pthreads in C/C++
- C++ Program to Perform Matrix Multiplication
- How to perform element-wise addition on tensors in PyTorch?
- Instructions to perform addition in 8085 Microprocessor
- How to perform Merge Sort using C#?
- How to combine two rows in R matrix by addition?
- How to perform matrix transformation in OpenCV Python?
- C++ Program to Perform LU Decomposition of any Matrix
- Write a C program to perform 3X3 matrix operations
- Python program addition of two matrix
- How to perform sort using Java?
- How to check whether the given matrix is a Toeplitz matrix using C#?
- How to perform Division of Exponents of Same Base using C#?
- How to perform Multiplication of Exponents of same base using C#?

Advertisements