Program to print the sum of boundary elements of a matrix | faceprep

Program to print the sum of boundary elements of a matrix is discussed here. Given a matrix, the task is to print the boundary elements of the matrix and display their sum.

For example, consider the matrix given below.

1    2    3                  
4    5    6 
7    8    9

Boundary Matrix

1    2    3                  
4           6 
7    8    9

Sum of boundary elements: 1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 = 40 

Algorithm to print the sum of boundary elements of a matrix

  • Input the order of the matrix.
  • Input the matrix elements.
  • Print all the boundary elements of the matrix.
  • Find the sum of the boundary elements.
  • Print sum.

Program to print the sum of boundary elements of a matrix is given below.

/* C Program to print the sum of boundary elements of a matrix */
#include<stdio.h>
#include<limits.h>

int main()
{
int m, n, sum = 0;
printf(“\nEnter the order of the matrix : “);
scanf(“%d %d”,&m,&n);
int i, j;
int mat[m][n];
printf(“\nInput the matrix elements\n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat[i][j]);
}

printf(“\nBoundary Matrix\n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if (i == 0 || j == 0 || i == n – 1 || j == n – 1)
{
printf(“%d “, mat[i][j]);
sum = sum + mat[i][j];
}
else
printf(” “);
}
printf(“\n”);
}
printf(“\nSum of boundary is %d”, sum);
}

/* C++ Program to print the sum of boundary elements of a matrix */
#include<iostream>
using namespace std;

int main()
{
int m, n, sum = 0;
cout << “\nEnter the order of the matrix : “;
cin >> m >> n;
int i, j;
int mat[m][n];
cout << “\nInput the matrix elements\n”;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat[i][j];
}

cout << “\nBoundary Matrix\n”;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if (i == 0 || j == 0 || i == n – 1 || j == n – 1)
{
cout << mat[i][j] << ” “;
sum = sum + mat[i][j];
}
else
cout << ” “;
}
cout << endl;
}
cout << “\nSum of boundary is ” << sum << endl;
}

/* Java Program to print the sum of boundary elements of a matrix */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int m, n, sum = 0, row1 = 0, col_n = 0, diag = 0;
System.out.print(“\nEnter the order of the matrix : “);
m = sc.nextInt();
n = sc.nextInt();
int i, j;
int[][] mat = new int[m][n];
System.out.print(“\nInput the matrix elements \n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
mat[i][j] = sc.nextInt();
}

System.out.print(“\nBoundary Matrix\n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if (i == 0 || j == 0 || i == n – 1 || j == n – 1)
{
System.out.print(mat[i][j] + ” “);
sum = sum + mat[i][j];
}
else
System.out.print(” “);
}
System.out.print(“\n”);
}
System.out.print(“\nSum of boundary is ” + sum);


}

}

# Python Program to print the sum of boundary elements of a matrix
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum = 0
m = 3
n = 3
print(“\nBoundary Matrix\n”)
for i in range (0, m):
for j in range (0, n):
if (i == 0 or j == 0 or i == n – 1 or j == n – 1):
print(mat[i][j], end = ” “)
sum = sum + mat[i][j]
else:
print(end = ” “)
print(“\n”)
print(“\nSum of boundary is “, sum)

#include<stdio.h>
#include<stdlib.h>
int main()
{
int **a,r,c,i,j;
scanf(“%d”,&r);
scanf(“%d”,&c);

a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
*(a+i)=(int*)malloc(c*sizeof(int));

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf(“%d”,*(a+i)+j);
}
}

i=0;int sum1=0;
for(j=0;j<c;j++)
sum1=sum1+*(*(a+i)+j);

i=r-1;int sum2=0;
if(i!=0)
{
for(j=0;j<c;j++)
sum2=sum2+*(*(a+i)+j);
}

j=0; int sum3=0;
for(i=1;i<r-1;i++)
sum3=sum3+*(*(a+i)+j);

j=c-1; int sum4=0;
for(i=1;i<r-1;i++)
sum4=sum4+*(*(a+i)+j);

printf(“Sum of boundary is %d”,sum1+sum2+sum3+sum4);

return 0;
}

Output:

sum of boundary elements of a matrix