codescracker
c

C Program to Find Largest Element in a Matrix



« C Tutorial C Examples »

In this tutorial we will learn about how to create a program in C that will find and print the largest element from a matrix. This program will ask from user to enter any 3*3 matrix, and then find out the largest element from that given 3*3 matrix (by user at run-time)

// Find Largest Element in a Matrix
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int mat[3][3], i, j, max;
    printf("Enter any 3*3 matrix: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            scanf("%d", &mat[i][j]);
    }
    max = mat[0][0];
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(max<mat[i][j])
                max = mat[i][j];
        }
    }
    printf("\nLargest Element = %d", max);
    getch();
    return 0;
}

As the above program was build and run under Code::Blocks IDE, therefore here is the sample run after successful build and run:

c program largest element in matrix

Now enter all the 9 elements of 3*3 matrix and press ENTER key to see the largest one from all the 9 numbers:

largest element in matrix c program

Program Explained


« C Tutorial C Examples »