### C# Basic

### C# Interview Programs

- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number

# C# Program to Perform Matrix Addition

Photo Credit to CodeToFun

## π Introduction

Matrix addition is a fundamental operation in linear algebra and computer science. It involves adding corresponding elements of two matrices to create a new matrix.

This operation is essential in various fields, including computer graphics, scientific computing, and data analysis.

In this tutorial, we'll explore a C# program that performs matrix addition.

Understanding matrix operations and implementing them in C can provide a solid foundation for more complex computations.

## π Example

Let's delve into the C# code that achieves matrix addition.

```
using System;
class MatrixAddition {
// Function to perform matrix addition
static int[, ] AddMatrices(int[, ] mat1, int[, ] mat2) {
int rows = mat1.GetLength(0);
int cols = mat1.GetLength(1);
int[, ] result = new int[rows, cols];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result[i, j] = mat1[i, j] + mat2[i, j];
}
}
return result;
}
// Function to display a matrix
static void DisplayMatrix(int[, ] matrix) {
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
// Driver program
static void Main() {
// Sample matrices for testing
int[, ] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[, ] matrix2 = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
// Call the function to perform matrix addition
int[, ] resultMatrix = AddMatrices(matrix1, matrix2);
// Displaying matrices and result
Console.WriteLine("Matrix 1:");
DisplayMatrix(matrix1);
Console.WriteLine("\nMatrix 2:");
DisplayMatrix(matrix2);
Console.WriteLine("\nResultant Matrix:");
DisplayMatrix(resultMatrix);
}
}
```

### π» Testing the Program

Feel free to replace the sample matrices with your own 3x3 matrices in the Main method to test the program with different input.

Matrix 1: 1 2 3 4 5 6 7 8 9 Matrix 2: 9 8 7 6 5 4 3 2 1 Resultant Matrix: 10 10 10 10 10 10 10 10 10

Compile and run the program to see the result of the matrix addition.

### π§ How the Program Works

- The program defines a class MatrixAddition containing static methods AddMatrices to add two matrices and DisplayMatrix to display a matrix.
- In the Main method, sample matrices matrix1 and matrix2 are provided for testing.
- The AddMatrices method is called to perform the addition, and the result is stored in resultMatrix.
- The matrices are displayed using the DisplayMatrix method.

## π§ Understanding the Concept of Matrix Addition

Matrix addition involves adding corresponding elements of two matrices to create a new matrix.

Given two matrices A and B, the sum C is calculated as follows:

C_{ij} = A_{ij} + B_{ij}.

where C_{ij} is the element at the i-th row and j-th column of matrix C, and A_{ij} and B_{ij} are the corresponding elements of matrices A and B.

Matrix addition is only defined for matrices of the same size; that is, they must have the same number of rows and columns.

## π’ Optimizing the Program

While the provided program is effective, there are optimizations that can be applied for larger matrices. Consider exploring techniques such as parallelization for improved performance.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

## π¨βπ» Join our Community:

## Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee
If you have any doubts regarding this article (

C# Program to Perform Matrix Addition), please comment here. I will help you immediately.