Wednesday, 15 July 2015

Java Programs Made Easy

Q)... Input a int array. Square the elements in even position and cube the elements in odd position and add them as result.
input: {1,2,3,4}
output: 1^3+2^2+3^3+4^2



  1. package Set1;
  2. public class ClassSet24 {
  3. public static int squaringAndCubingOfElements(int[] a){
  4.  int n1=0,n2=0;
  5.  for(int i=0;i<a.length;i++)
  6.   if(i%2==0)
  7.    n1+=(a[i]*a[i]*a[i]);
  8.   else
  9.    n2+=(a[i]*a[i]);
  10.  return (n1+n2);
  11. }
  12. public static void main(String[] args) {
  13.   int a[]={1,2,3,4,5,6};
  14.   System.out.println(squaringAndCubingOfElements(a));
  15.  }
  16. }