Matrix in java program

Computer Science Level pending

the output of the program below is a matrix. how the results of the sum of the diagonal matrix

1
2
3
4
5
6
7
8
int n[][] = {{2,4,8},{5,3,1},{0,7,9}};

for (j = 0; 3 > j; j++) {
    System.out.print("|");
    for (int k = 0; 3 > k; k++) {
        System.out.print(" " + n[j][k] + " ");}
        System.out.println("|");
    }


The answer is 14.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Raj Maiti
Mar 22, 2014

result will b 14

Chenna Kesavulu
Mar 21, 2014

class Matrix { public static void main(String ar[]) { int sum=0; int n[][] = {{2,4,8},{5,3,1},{0,7,9}};

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
    if(((i==0)&&(j==0))||((i==1)&&(j==1))||((i==2)&&(j==2)))
    sum+=n[i][j];
}
}

System.out.println(sum); } }

this is solution for previous question which is sum of diagonal matrix

chenna kesavulu - 7 years, 2 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...