Array

8. Program

How to read first half and second half elements seperately ?

class O { public static void main(String args[]) { int[] a = {10, 2, 5, 3, 9, 2, 8, 10, 11, 20}; int mid = a.length / 2; for(int i = 0; i < mid ; i++) { System.out.println(a[i]); } System.out.println("--------"); for(int i = mid - 1; i < a.length ; i++) { System.out.println(a[i]); } } }

9. Program

How to read first half odd indexed and second half even indexed elements seperately ?

class P { public static void main(String args[]) { int[] a = {10, 2, 5, 3, 9, 2, 8, 10, 11, 20, 10, 2, 5, 3, 9, 2, 8, 10, 11, 20}; int mid = a.length / 2; for(int i = 1; i < mid ; i+=2) { System.out.println("Odd element :" + a[i]); } System.out.println("--------"); if(mid % 2 != 0) { mid++; } for(int i = mid; i < a.length ; i+=2) { System.out.println("Even element :" + a[i]); } } }

10. Program

How to read first half even and second half odd indexed elements seperately ?

class Q { public static void main(String args[]) { int[] a = {10, 2, 5, 3, 9, 2, 8, 10, 11, 20, 10, 2, 5, 3, 9, 2, 8, 11, 20, 10}; int mid = a.length / 2; for(int i = 0; i < mid ; i+=2) { System.out.println("Even element :" + a[i]); } System.out.println("--------"); if(mid % 2 == 0) { mid++; } for(int i = mid; i < a.length ; i+=2) { System.out.println("Odd element :" + a[i]); } } }

Page....