Write Method Takes Integer Array Parameter Replaces Odd Number Array S Value Times 2 Publi Q37077927

Write a method that takes an integer array parameter andreplaces each odd number in the array with it’s value times 2.public void doubleOdd(int [] arr); So this method would change{3,4,5,8,9} into {6,4,10,8,18} Write a main method to test theabove method


Solution


ANSWER:-

class Test
{
   public static void doubleOdd(int [] arr)
   {
       for(inti=0;i<arr.length;i++)
       {
           if(i%2==0)
           {
              arr[i]=arr[i]*2;
           }
       }
   /*   for(int i=0;i<arr.length;i++) // usefor loop for print output
       {
          System.out.print(arr[i]+” “);
       }*/
   }
   public static void main (String[] args)
   {
       int arr[]={3,4,5,8,9};
       doubleOdd(arr);
   }
}

// OUTPUT

Success #stdin #stdout 0.04s 21841 92KB 6 4 10 8 18

Success

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.