Array

17. Program

(Imp_Q) Comparing the element with average value ?

class X { public static void main(String args[]) { int[] a = {10, 2, 5, 3, 9, 2, 8, 10, 11, 20, 10}; int total = 0; double sum = 0; double avg = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; total++; } avg = sum / total; for(int i = 0; i < a.length; i++) { if(a[i] > avg) { System.out.println("index value" + i + ", element of index" + a[i] + " is greater than " + avg); } else { System.out.println("index value" + i + ", element of index" + a[i] + " is less than " + avg); } } } }

18. Program

Find out an array left strength or right strength ?

class Y // Find out an array left strength or right strength? { public static void main(String args[]) { int[] a = {10, 2, 5, 3, 9, 2, 8, 10, 11, 20, 10}; int leftStrength = 0, rightStrength = 0; int mid = a.length / 2; for(int i = 0; i < mid; i++) { leftStrength += a[i]; } for(int i = mid; i < a.length; i++) { rightStrength += a[i]; } if(leftStrength < rightStrength) { System.out.println("Left Strength : " + leftStrength + " is less than Right Strength : " + rightStrength); } else if(leftStrength > rightStrength) { System.out.println("Left Strength : " + leftStrength + " is greater than Right Strength : " + rightStrength); } else if(leftStrength == rightStrength) { System.out.println("Left Strength : " + leftStrength + " is equal to Right Strength : " + rightStrength); } } }

19. Program

(Imp_Q) Find out the sequential three indexes, where sum of first and second is third ?

class A { public static void main(String args[]) { int[] a = {1, 2, 3, 2, 3, 4, 11, 13, 24}; int x, y, z; for(int i = 0; i < a.length - 2; i++) { x = a[i]; y = a[i+1]; z = a[i+2]; if((x+y) == z) { System.out.println(x); System.out.println(y); System.out.println(z); } } } }

Page....