Hello Friends, In this post, i am going to write a C# Program to find Largest element in a Matrix.

C# Program to find Largest Element in a Matrix:

Open Visual Studio and create a Console Application Project. Write the following code in Program.cs file.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FindLargestElementDemo
{
class Program
{
int[,] _iMatrix;
public Program()
{
_iMatrix = new int[,] { { 12, 21, 63 }, { 40, 15, 6 } };
}
void PrintMatrix()
{
Console.WriteLine("Elements in the Given Matrix : ");
for (int iCount = 0; iCount < 2; iCount++)
{
for (int jCount = 0; jCount < 3; jCount++)
{
Console.Write(_iMatrix[iCount, jCount] + "\t");
}
Console.WriteLine("\n");
}
}
int FindMax()
{
int iLargest = _iMatrix[0, 0];
for (int iCount = 0; iCount < 2; iCount++)
{
for (int jCount = 0; jCount < 3; jCount++)
{
if (iLargest < _iMatrix[iCount, jCount])
{
iLargest = _iMatrix[iCount, jCount];
}
}
}
return iLargest;
}
static void Main(string[] args)
{
Console.WriteLine("C# Program to find Largest Element in a Matrix");
Program objProgram = new Program();
objProgram.PrintMatrix();
Console.WriteLine("Largest Element : {0}", objProgram.FindMax());
Console.ReadLine();
}
}
}

Output:

Find Largest Element in a Matrix

View More:

Conclusion:

Hope you understand how to find Largest Element in a Matrix using C#. Your Feedback, Comments and suggests are always welcome.

Thank You.

Leave a Comment