
- 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 add two matrices
Firstly, set three arrays.
int[, ] arr1 = new int[20, 20]; int[, ] arr2 = new int[20, 20]; int[, ] arr3 = new int[20, 20];
Now users will enter values in both the matrices. We have to set the row and size columns as n=3, since we want a square matrix of 3x3 size i.e 9 elements.
Add both the matrices and print the third array that has the sum.
for(i=0;i<n;i++) for(j=0;j<n;j++) arr3[i,j]=arr1[i,j]+arr2[i,j];
The following is the complete code to add two matrices in C#.
Example
using System; public class Exercise19 { public static void Main() { int i, j, n; int[, ] arr1 = new int[20, 20]; int[, ] arr2 = new int[20, 20]; int[, ] arr3 = new int[20, 20]; // setting matrix row and columns size n = 3; Console.Write("Enter elements in the first matrix:
"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { arr1[i, j] = Convert.ToInt32(Console.ReadLine()); } } Console.Write("Enter elements in the second matrix:
"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { arr2[i, j] = Convert.ToInt32(Console.ReadLine()); } } Console.Write("
First matrix is:
"); for (i = 0; i < n; i++) { Console.Write("
"); for (j = 0; j < n; j++) Console.Write("{0}\t", arr1[i, j]); } Console.Write("
Second matrix is:
"); for (i = 0; i < n; i++) { Console.Write("
"); for (j = 0; j < n; j++) Console.Write("{0}\t", arr2[i, j]); } for (i = 0; i < n; i++) for (j = 0; j < n; j++) arr3[i, j] = arr1[i, j] + arr2[i, j]; Console.Write("
Adding two matrices:
"); for (i = 0; i < n; i++) { Console.Write("
"); for (j = 0; j < n; j++) Console.Write("{0}\t", arr3[i, j]); } Console.Write("
"); } }
Output
Enter elements in the first matrix: Enter elements in the second matrix: First matrix is: 000 000 000 Second matrix is: 000 000 000 Adding two matrices: 000 000 000
- Related Articles
- Java program to add two matrices.
- Golang Program To Add Two Matrices
- How to write Java program to add two matrices
- C++ program to overload addition operator to add two matrices
- How to Add Two Matrices using Python?
- C# program to multiply two matrices
- Java program to subtract two matrices.
- Java program to multiply two matrices.
- Python program to multiply two matrices
- Program to multiply two matrices in C++
- Program for subtracting two matrices.
- C++ Program to Check Multiplicability of Two Matrices
- How can Tensorflow be used to add two matrices using Python?
- C# program to check if two matrices are identical
- Java program to check if two given matrices are identical

Advertisements