Square Matrix - Corner Elements Sum Java PART-005
Square Matrix - Corner Elements Sum:
A square matrix of size N×N is passed as the input. The program must calculate and print the sum of the elements in the corners.
Program in Java:
*********************************************************************************
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();int s=0;
int[][] d=new int[x][x];
for(int i=0;i<x;i++)
for(int j=0;j<x;j++)
{
d[i][j]=sc.nextInt();
if((i==0&& j==0) ||(i==0 && j==x-1)||(i==x-1&& j==0)||(i==x-1 && j==x-1))
s+=d[i][j];
}
System.out.print(s);
}
}
*********************************************************************************
Comments
Post a Comment