Page 371 - CA_Blue( J )_Class10
P. 371

When you execute the preceding program, it will ask for value to array.
                 You need to enter the values between the curly brackets separated by
                 comma as shown:
                 After entering the values, when you click on the OK button, following
                 output will appear:
                 Elements of mat[][] array are:
                 1  3  6

                 6  9  7
                 4  6  9
                 •  Taking Values from User: As you know that the Scanner class is used to take the values form runtime. It provides
                   different methods to take different types of values. Let us take an example.

                  Program 6    Create a two-dimensional array of size 2 x 3 and print the square of all the odd numbers in the array.


                   1  import java.util.*;

                   2  class array_odd
                   3  {

                   4      public static void main()
                   5      {

                   6          Scanner sc= new Scanner(System.in);
                   7          int ar[][]=new int[2][3];

                   8          int i,j;
                   9          for (i=0; i<2; i++)

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

                  12      {
                  13         System.out.print("Enter a number : ");

                  14                    ar[i][j] =sc.nextInt();
                  15                }

                  16         }
                  17         for (i=0; i<2; i++)

                  18          {
                  19      for (j=0; j<3; j++)

                  20      {
                  21         if(ar[i][j] %2==1)
                  22         {

                  23               System.out.println("Odd Number: "+ar[i][j]+ " and its square is:");
                                  System.out.println(ar[i][j]*ar[i][j]);



                                                                                                                       369
                                                                                                              Arrays   369
   366   367   368   369   370   371   372   373   374   375   376