Sum Of Border Elements

Sum Of Border Elements


Write a program to display the sum of border elements in a m x n matrix.

import java.io.*; class SumOfBorderElements { public static void main(String ar[])throws Exception { BufferedReader ob=new BufferedReader( new InputStreamReader(System.in)); int i,j,sobe=0,m,n; System.out.println("Enter the no of rows and columns of the matrix : "); m=Integer.parseInt(ob.readLine()); n=Integer.parseInt(ob.readLine()); int a[][]=new int[m][n]; System.out.println("\nEnter "+(m*n)+" elements : "); for(i=0;i< m;i++) { for(j=0;j< n;j++) { a[i][j]=Integer.parseInt(ob.readLine()); } } for(i=0;i< m;i++) { for(j=0;j< n;j++) { if(i==0 || j==0 || i==m-1 || j==n-1) sobe+=a[i][j]; } } System.out.println("\nSum of border elements : "+ sobe); } }

Have something to say? Log in to comment on this post.

0 comments