Page 299 - Computer science 868 Class 12
P. 299

The merged array is
                 1      2      3      4      5       6      7      8      9

                     9.4 LENGTH OF THE ARRAY
                 To find the total number of elements in the array, we use the “length” method.

                 Syntax:
                    int len= name_of_array.length;
                 Here, len will contain the length of the array.


                   Program 12    Write a program in Java to assign n numbers in a single-dimensional array arr[]. Transfer and
                                 store all the even numbers in an array even[] while all the odd numbers in another array
                                 odd[]. Finally, print the elements of both the arrays.

                   1       import java.util.*;
                   2       class even_odd

                   3       {
                   4           public static void main(String args[])

                   5           {
                   6               Scanner in = new Scanner(System.in);

                   7               int i = 0, e = 0, od = 0;;
                   8               int arr[] = {2,3,11,45,37,89,54,44,346,86};

                   9               int even[] = new int[10];
                   10              int odd[] = new int[10];

                   11              for (i = 0; i < arr.length; i++)
                   12              {

                   13                  if (arr[i] % 2 == 0)
                   14                      even[e++] = arr[i];

                   15                  else
                   16                      odd[od++] = arr[i];

                   17              }
                   18              System.out.print("\nTotal Numbers:");

                   19              for (i = 0; i < arr.length; i++)
                   20              {
                   21                  System.out.print(arr[i] + " ");

                   22              }

                   23              System.out.print("\nEven Numbers:");
                   24              for (i = 0; i < e; i++)





                                                                                                                       297
                                                                                                              Arrays   297
   294   295   296   297   298   299   300   301   302   303   304