Java Program that interchanges the odd and even elements of an array.

//Java Program that interchanges the odd and even elements of an array.
public class Program 
{   
    	public static void main( String[] args ) 
    	{
		int a[] = {1,2,3,4,5,6},i,tmp;
		for (i = 0; i <= 5; i = i + 2)
		{
			tmp = a[i];
			a[i] = a[i + 1];
			a[i + 1] = tmp;
		}
		for (i = 0; i <= 5; i++)
		{
			System.out.print(a[i] +"\n");
		}
    	}	
}

Output :
2
1
4
3
6
5



Leave a comment