Page 466 - CA_Blue( J )_Class10
P. 466
2 class array_sum
3 {
4 public static void arr()
5 {
6 Scanner sc= new Scanner(System.in);
7 int n,i,sum=0; // Declaration of Variable
8 System.out.println("Enter size of array : ");
9 n=sc.nextInt(); // Accepting size of Array
10 int ar[] = new int[n]; // Creating array of size n
11 for(i=0; i<n; i++) // Taking values in the array
12 {
13 System.out.print("Enter a number : ");
14 ar[i] = sc.nextInt();
15 }
16 System.out.print("Even Numbers in Odd Position are : " );
17 for(i=0;i<n;i++) // Finding the required array elements
18 {
19 if(ar[i]%2==0 && i%2 != 0)
20 {
21 System.out.print(ar[i]+ " ");
22 sum = sum+ar[i]; // Sum of the elements
23 }
24 }
25 System.out.println("\nThe sum is : "+sum);
26 } //Close of main()
27 } //Close of class
Output
Enter size of array :
5
Enter a number : 1
Enter a number : 2
Enter a number : 3
Enter a number : 4
Enter a number : 5
Even Numbers in Odd Position are : 2 4
The sum is : 6
464464 Touchpad Computer Applications-X

