write a C# Program add two matrix ?



write a C# Program add two matrix ?..

Answer / Alok Mehta

Here's an example of a simple console application that adds two matrices in C#:

```csharp
using System;

namespace MatrixAddition
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows for both matrices: ");
int rows = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of columns for both matrices: ");
int cols = Convert.ToInt32(Console.ReadLine());

if (rows != cols)
{
Console.WriteLine("Error! Matrices cannot be added as they have different dimensions.");
return;
}

int[,] matrix1 = new int[rows, cols];
int[,] matrix2 = new int[rows, cols];
int[,] resultMatrix = new int[rows, cols];

Console.WriteLine("Enter elements of first matrix:");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix1[i, j] = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter elements of second matrix:");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix2[i, j] = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
resultMatrix[i, j] = matrix1[i, j] + matrix2[i, j];

Console.WriteLine("Result Matrix:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
Console.Write(resultMatrix[i, j] + " ");
Console.WriteLine();
}
}
}
}
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Sharp Interview Questions

Can an interface extend a class c#?

1 Answers  


When To use HashTable In C#

1 Answers   Infosys,


What are the ways in which client can create object on server in cao model?

1 Answers  


Why do you call it a process? What’s different between process and application in .net, not common computer usage, terminology?

1 Answers  


What is the Difference between value and reference type?

12 Answers   Wipro,


Can you store multiple data types in System.Array?

7 Answers  


What is data dictionary in c#?

1 Answers  


What is application c#?

1 Answers  


Why does my windows application pop up a console window every time I run it?

1 Answers  


Explain about Oops concept

1 Answers   Digital GlobalSoft,


Where are all .NET Collection classes located ?

8 Answers   TCS,


How long will it take to learn c#?

1 Answers  


Categories