Page 366 - CA_Blue( J )_Class10
P. 366

You will get the following output:
              Length of array is: 10


              Taking Values from User
              There are two ways to take the values from the user which are:
              •  Using Method Argument: Using this method, you can take the values of an array as parameter at the time of
                 program execution. In this method, the array is passed as an argument to the main() method.


                Program 2    To find the sum of all the elements of an array.


                 1  public class even
                 2  {

                 3      public static void main(int even[])
                 4         {

                 5             int sum = 0, i = 0;

                 6             for(i = 0; i < even.length; i++)
                 7             {
                 8                 sum = sum + even[i];

                 9              }

                10              System.out.println("The sum of array elements is: " + sum);
                11          }
                12  }


              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:











              •  Using Scanner Class: As you know, the Scanner class is used to take the values form runtime. It provides different
                 methods to take different types of values.


                Program 3    To find the sum of odd elements of an array.


                 1  import java.util.*;
                 2  public class oddsum





                364364  Touchpad Computer Applications-X
   361   362   363   364   365   366   367   368   369   370   371