Page 367 - CA_Blue( J )_Class10
P. 367

3  {
                   4      public static void main()

                   5      {
                   6          int arr[] = new int[5];

                   7          int sum = 0, i;

                   8          Scanner sc = new Scanner(System.in);
                   9          System.out.println("Enter five values for array: ");
                  10          for(int count = 0; count < 5; count++)

                  11          {
                  12              arr[count] = sc.nextInt();

                  13          }
                  14          for(i = 0; i < arr.length; i++)

                  15          {
                  16              if(arr[i]%2!=0)

                  17              sum = sum + arr[i];
                  18          }

                  19          System.out.println("The sum of odd elements of array is: " + sum);
                  20      }

                  21  }

                 When you run the preceding program, it will ask to enter array elements from runtime. After entering the values, when
                 you press the Enter key, following output will appear:

                 Enter five values for array:
                 2

                 3
                 4
                 7
                 9

                 The sum of odd elements of array is: 19

                  Program 4    To input five numbers and print the sum and average of the even numbers present in the array.


                   1  import java.util.*;

                   2  class array_sum_avg
                   3  {

                   4      public static void main()





                                                                                                                       365
                                                                                                              Arrays   365
   362   363   364   365   366   367   368   369   370   371   372