Page 296 - computer science (868) class 11
P. 296

Using Direct Input Method
              In this method, the values to be inserted are taken in as parameters by the user.


                Program 20     Create an array of size 4 × 4, take values using parameter and display the diagonals and also
                               display the sum of numbers present in the diagonal position.

                 1       class sum_diagonal
                 2       {

                 3           public static void main(int a[ ][ ])
                 4           {
                 5               int i, j, sum = 0;

                 6               for(i = 0; i<4; i++)

                 7               {
                 8                   for(j = 0; j<4; j++)
                 9                   {

                10                       if(i == j || (i+j) == 3)
                11                       {

                12                           System.out.print (a[i][j]+" ");
                13                           sum += a[i][j];

                14                       }
                15                       else

                16                       System.out.print (" ");
                17                   }

                18                   System.out.println ();
                19               }

                20               System.out.println ("Sum of diagonals:"+sum);
                21           }

                22       }

              To take value from the user,





















                294294  Touchpad Computer Science-XI
   291   292   293   294   295   296   297   298   299   300   301