Q:

C# program to find the largest element in the matrix

belongs to collection: C# Basic Programs | array programs

0

C# program to find the largest element in the matrix

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to multiply two matrices... >>
<< C# program to check the matrix is an identity matr...