Question
program to find sum of non-diagonal elements of matrix
Matrix = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
SUM OF NON-DIAGONAL ELEMENTS OF MATRIX:20
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class SumOfNonDiagonalElementsOfMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0,sum=0;
int a[][];
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE SIZE OF MATRIX");
size=sc.nextInt();
a=new int[size][size];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
if(i!=j && (i+j!=size-1))
{
sum=sum+a[i][j];
}
}
}
System.out.println("SUM OF NON-DIAGONAL ELEMENTS OF MATRIX:"+sum);
}
}