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 |
Can an interface extend a class c#?
When To use HashTable In C#
What are the ways in which client can create object on server in cao model?
Why do you call it a process? What’s different between process and application in .net, not common computer usage, terminology?
What is the Difference between value and reference type?
Can you store multiple data types in System.Array?
What is data dictionary in c#?
What is application c#?
Why does my windows application pop up a console window every time I run it?
Explain about Oops concept
Where are all .NET Collection classes located ?
How long will it take to learn c#?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)