Friday, 17 July 2015

Java Programming

Q). Retrieve the odd-position digits from input integer array. Multiply them with their index and return their sum.
  1.  
  2. package Set1;
  3. public class ClassSet43 {
  4.       public static void main(String[] args) {
  5.             int[] a={5,1,6,2,9,4,3,7,8};
  6.             System.out.println("Sum Of Odd Position Elements Multiplied With Their Index Is:");
  7.             System.out.println(retrievalOfOddPositionElements(a));
  8.       }
  9.       public static int retrievalOfOddPositionElements(int[] a) {
  10.             int s=0;
  11.             for(int i=0;i<a.length;i++)
  12.                   if(i%2!=0)
  13.                         s+=a[i]*i;
  14.             return s;
  15.       }
  16. }