ADVERTISEMENT
ADVERTISEMENT

C# program to find the largest element in the matrix

Here, we are going to learn how to find the largest element in the matrix in C#?
Submitted by Nidhi, on November 02, 2020

Here, we will read a matrix from the user and then find the largest elements in the matrix.

Program:

The source code to find the largest element in the matrix is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the largest element in the matrix.

using System;

class MatrixDemo
{
    public static void Main(string[] args)
    {
        int i = 0;
        int j = 0;

        int row     = 3;
        int col     = 3;
        int large   = 0;

        int[,] Matrix= new int[row, col];
        
        Console.Write("Enter the elements of matrix: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Matrix[i, j] = int.Parse(Console.ReadLine());

                if (large < Matrix[i, j])
                    large = Matrix[i, j];
            }
        }
        
        Console.WriteLine("\nMatrix: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Console.Write(Matrix[i, j] + "\t");

            }
            Console.WriteLine();
        }

        Console.WriteLine("Largest element is : "+large);
    }
}

Output:

Enter the elements of matrix: 1
2
3
4
5
6
7
8
9

Matrix:
1       2       3
4       5       6
7       8       9
Largest element is : 9
Press any key to continue . . .

Explanation:

In the above program, we created a class MatrixDemo that contains a Main() method. The Main() method is the entry point for the program, Here, we created a 2-D array to represent a matrix.

Console.Write("Enter the elements of matrix: ");
for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Matrix[i, j] = int.Parse(Console.ReadLine());

        if (large < Matrix[i, j])
            large = Matrix[i, j];
    }
}

In the above code, we read the elements of the matrix and find the largest element by comparing each element with the variable large, if the element is greater than large then assigned the largest value to the variable large. That's why we will get the largest element after inputting all elements.

Console.WriteLine("\nMatrix: ");
for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Console.Write(Matrix[i, j] + "\t");

    }
    Console.WriteLine();
}

Console.WriteLine("Largest element is : "+large);

In the above code, we printed the elements of the matrix and the largest element of the matrix on the console screen.

C# Basic Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.