• Register
1 vote

Problem :

I am very new to programming. I need to write a program called swapAdj that must accept the array of integers and after that swap those elements at the adjacent indexes. For example consider the elements 0 and 1 are swapped by the program and after that elements 2 and 3 are also swapped by the program and after that so on. Also please note if your array has the odd length then the program must keep final element unmodified.

Please help me in writing above program.

8 5 2
3,230 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution :

I also wanted to write similar program and after doing the lot of trial and errors I was able to finally write the error free program. Please find below the program for your help.

public static void swapAdj(int[] a){

    int swaplen=a.length;

        if(swaplen %2 ==0){

            for(int i=0; i<swaplen; i=i+2){

                int swapc=a[i]+a[i+1];

                a[i]= swapc -a[i];

                a[i+1]= swapc -a[i+1];



        if(swaplen %2 !=0){

            for(int j=0; j<swaplen -1; j=j+2){

                int swapc=a[j]+a[j+1];

                a[j]= swapc-a[j];

                a[j+1]= swapc-a[j+1];





public static void printSwapArray(int[] a){

    int swaparrlen=a.length;

    for(int i=0;i<swaparrlen;i++)

    System.out.print(a[i]+" ");


I hope you have read the entire swapAdj  program and also you are able to understand the logic of the above written program.

8 4 1
5,680 points

Related questions

0 votes
1 answer 3 views
Problem: Anyone here who know nested loops to print a rectangle. sample output for given program? If yes, then please answer me.
asked Mar 27 niyatiry 3.6k points
0 votes
1 answer 6 views
Problem: Can anyone help me about this? If anyone know about loop to print each contact and contact email. Then please read my question carefully and answer me.
asked Mar 26 niyatiry 3.6k points
0 votes
1 answer 1.3K views
Problem : I am getting bellow error can only iterate over an array or an instance of java.lang.iterable
asked Nov 1, 2019 peterlaw 6.9k points
0 votes
1 answer 24 views
Problem: How can I find the values in an array whose sum equals some given value?
asked Feb 27 Code Learner 9.5k points
1 vote
1 answer 11 views
Problem: I want to make a Java program to find the repeated digits in an integer without the use of string or array method . Can somebody do this so I can understand how to make this program? Thank you !!
asked Mar 17 Raj Tyagi 6.3k points
0 votes
1 answer 13 views
Problem: Please assist me in resolving this issue: find a pair of elements from an array whose sum equals a given number in java
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 14 views
Problem: Hello! I am trying to write a program that takes input from the user and determines whether the user entered a number, alphabet, or a special character. I am a beginner so, facing some difficulty in understanding the logic. Can somebody from here help me find out the solution? Thanks!
asked Feb 25 Code Learner 9.5k points
0 votes
1 answer 4 views
Problem: How do I find > column name from a given column number
asked Mar 20 Azlan2020 2.3k points
0 votes
1 answer 5 views
Problem: The duplicate occurrences of values from the following list (array) need to be removed: 7, 9, 3, 1, 7, 8, 9, 1, 9, 11, 10 I have written the following code in an attempt to do the job: class Main { public static void main (String[] args) ... array. I do not understand what I am doing wrong and how to get the desired result without worrying about the order of the values. Any suggestions?
asked Feb 27 Code Learner 9.5k points
0 votes
1 answer 2 views
Problem: How can I swap elements in an array java? Help me.
asked Mar 31 zayed1 35.6k points