Page 384 - ComputerScience_Class_11
P. 384

Program 13     Write a Java program to store data in a matrix 4 × 4 and display the elements of the matrix
                               by replacing each element of the left diagonal with zero.

                1       import java.util.*;

                2       class diagonal
                3       {

                4           public static void main(String args[])
                5           {
                6               Scanner sc= new Scanner(System.in);

                7               int i, j;
                8               int ar[ ][ ]=new int[4][4];

                9               for (i=0; i<4; i++)
                10              {
                11                   for(j=0; j<4; j++)

                12                   {
                13                         System.out.print("Enter a number: ");
                14                         ar[i][j] =sc.nextInt();

                15                   }
                16              }

                17              System.out.println("4x4 matrix is");
                18              for(i=0; i<4; i++)
                19              {

                20                   for(j=0; j<4; j++)
                21                   {
                22                         System.out.print(ar[i][j]+" ");

                23                   }
                24                   System.out.println();

                25              }
                26              for(i=0; i<4; i++)
                27              {

                28                   ar[i][i]=0;
                29              }

                30              System.out.println("New elements are");
                31              for (i=0; i<4; i++)
                32              {

                33                   for(j=0; j<4; j++)





                  382  Touchpad Computer Science (Ver. 3.0)-XI
   379   380   381   382   383   384   385   386   387   388   389