You are here

C program to find maximum element in a matrix

C programming code to find the largest or the maximum element in a matrix.

C code

  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. main()
  5. {
  6.    int m, n, c, d, matrix[10][10], maximum;
  7.  
  8.    printf("Enter the number of rows and columns of matrix\n");
  9.    scanf("%d%d",&m,&n);
  10.    printf("Enter the elements of matrix\n");
  11.  
  12.    for( c = 0 ; c < m ; c++ )
  13.    {
  14.       for( d = 0 ; d < n ; d++ )
  15.       {
  16.          scanf("%d",&matrix[c][d]);
  17.       }
  18.    }
  19.    
  20.    maximum = matrix[0][0];
  21.  
  22.    for( c = 0 ; c < m ; c++ )
  23.    {
  24.       for( d = 0 ; d < n ; d++ )
  25.       {
  26.          if ( matrix[c][d] > maximum )
  27.             maximum = matrix[c][d];
  28.       }
  29.    }
  30.  
  31.    printf("Maximum element in matrix is %d\n", maximum);
  32.  
  33.    getch();
  34.    return 0;
  35. }