Page 378 - ComputerScience_Class_11
P. 378

Program 11     Write a Java program to store the numbers in a 3 × 3 matrix and assign the transposed matrix
                               in another array and display it. 'Transpose' means the interchanging of the rows and columns.
                               Original matrix

                                 1    2     3
                                 4    5     6
                                 7    8     9
                               Transposed matrix
                                 1    4     7
                                 2    5     8
                                 3    6     9


                1       import java.util.*;
                2       class array_double_transpose
                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[3][3];
                9               int tar[ ][ ]=new int[3][3];

                10              for (i=0; i<3; i++)
                11              {

                12                   for(j=0; j<3; j++)
                13                   {

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

                16                   }
                17              }

                18              System.out.print("Original Array:\n ");
                19              for (i=0; i<3; i++)

                20              {
                21                   for(j=0; j<3; j++)

                22                   {
                23                         System.out.print(ar[i][j]+" ");
                24                   }

                25                   System.out.println();





                  376  Touchpad Computer Science (Ver. 3.0)-XI
   373   374   375   376   377   378   379   380   381   382   383