Page 320 - Cs_withBlue_J_C11_Flipbook
P. 320
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,
318318 Touchpad Computer Science-XI

