C Program to Find Largest Element in a Matrix

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

#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 built and run in the Code::Blocks IDE, here is the sample run after a successful build and run:

c program largest element in matrix

Now enter all nine elements of the 3*3 matrix and press the ENTER key to see which of the nine numbers is the largest:

largest element in matrix c program

Program Explained

C Quiz


« Previous Program Next Program »


Liked this post? Share it!