title: java assign even elements to even index and odd to odd places and if the numbers are not equal add zeros to the places
body: <p>I am trying to write code to display the even elements to even indexes and odd to odd indexes and if the numbers added numbers are same then add zeros accordingly.</p>
  <p>Example:</p>
  <p>x = [1,2,3,4]
  output: 2 1 4 3
  x = [1 1 1 4]
  output: 4 1 0 1 0 1</p>
  <p>I reached to get even and odd positions but stuck after that.</p>
  <p>Below is my code.</p>
  <pre><code>import java.util.*;
  
  class ArrayDemo3 {
      
      public static void main(String[] args) {
          Scanner s = new Scanner(System.in);
          System.out.println(&quot;Enter Size of Array :: &quot;);
          int size = s.nextInt();
          int[] x = new int[size];
  
          System.out.println(&quot;Array Created having the size :: &quot; + size);
          System.out.println(&quot;Enter Elements for Array :: &quot;);
          for (int i = 0; i &lt; size; i++) {
              System.out.println(&quot;Enter element no-&quot; + (i + 1) + &quot; ::&quot;);
              x[i] = s.nextInt();
          }
  
          System.out.println(&quot;Contents of Array ::&quot;);
          for (int i = 0; i &lt; size; i++) {
              System.out.print(x[i] + &quot;  &quot;);
          }
          
          for (int i = 0; i &lt; size; i = i + 1) {
              int even = 0;
              int odd = 1;
              if (i &lt; size &amp;&amp; x[i] % 2 == 0) {
                  System.out.print(&quot;even : &quot;);
                  even = even + i;
                  System.out.print(&quot;position&quot; + i + &quot; &quot; + x[i] + &quot;  &quot;);
              } else {
                  System.out.print(&quot;odd : &quot;);
                  odd = odd + i;
                  System.out.print(i + &quot; &quot; + x[i] + &quot;  &quot;);
              }
              if (even &lt; size &amp;&amp; odd &lt; size) { 
                  int temp = x[even]; 
                  x[even] = x[odd]; 
                  x[odd] = temp; 
              } else {
                          
              }
              //System.out.print(x[i] + &quot;  &quot;);
          }
      }
  
  }
       
  </code></pre>