Page 593 - ComputerScience_Class_11
P. 593

17                  }

                   18              }

                   19              for (int i=0;i<n;i++)
                   20              {
                   21                  for (int j=0;j<n;j++)

                   22                  {
                   23                      // Condition for principal diagonal

                   24                      if (i == j)
                   25                          pd=pd+a[i][j];

                   26                      // Condition for secondary diagonal
                   27                      if ((i + j) == (n - 1))

                   28                          sd=sd+a[i][j];
                   29                  }

                   30                  sum=pd+sd;
                   31              }

                   32              System.out.println("Sum of Diagonals:"+ sum);
                   33          }

                   34
                   35          //Main method

                   36          public static void main (String args[])
                   37          {
                   38              sumofdiagonals ob= new sumofdiagonals();

                   39              ob.printDiagonalSums();

                   40          }
                   41      }


                 The output of the preceding program is as follows:
                       BlueJ: Terminal Window - Java

                   Options

                  enter the size of the matrix
                  2
                  enter the matrix elements
                  1
                  2
                  3
                  4
                  Sum of Diagonals:10




                                                                                                Internal Assessment  591
   588   589   590   591   592   593   594   595   596   597   598