Search the Blog

Sunday, August 18, 2019

JAVA program to return product of array element

Implement a function in JAVA of given array of integers, returns a new array for which every index carries the value of the product of the remaining elements.



Given array [2,3,1,4,5] it would return [60,40,120,30,24]


JAVA program to return product of array element



public class Main {

   List<Integer> arrInput1[];
   static  List<Integer> arrOutput1[];
   static int n;
   public static void main(String[] ar)
   {
   System.out.println("Enter the array size");
   Scanner a_dizsweb= new Scanner(System.in);
   n=a_dizsweb.nextInt();
   ArrayList<Integer> arrInput1=new ArrayList<Integer>();
   ArrayList<Integer>    arrOutput1=new ArrayList<Integer>();
     System.out.println("Enter the array size  ");

    for (int i=0;i<n;i++)
    {
          arrInput1.add(i, new Scanner(System.in).nextInt());
    }

findSpecialProduct(arrInput1);

    }
    public static void findSpecialProduct(List<Integer> inputArray) {
   ArrayList<Integer> arrOutput1=new ArrayList<Integer>();
         for(int k=0; k<n;k++)
      {
          int indexvalue1=1;
          int index=k;
       for (int i =0; i<n;i++)
       {
           if (index==i)
           {
             indexvalue1=indexvalue1*1;
           }
           else
           {
            indexvalue1=indexvalue1*inputArray.get(i); 
           }
       }
       arrOutput1.add(indexvalue1);
      }
/*     
 for(Integer inta : arrOutput) {
            System.out.println(inta.get());
        }*/
        arrOutput1.forEach(System.out::println);
   
    }

}

Implement a function which, given an array of integers, returns a new array for which every index carries the vale of the product of the remaining elements.

No comments:

Post a Comment

Translate