Skip to content

Instantly share code, notes, and snippets.

@mounicasrinivas
Created December 24, 2023 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mounicasrinivas/e1f161f7c534c0ea27c6f93ad49a2f75 to your computer and use it in GitHub Desktop.
Save mounicasrinivas/e1f161f7c534c0ea27c6f93ad49a2f75 to your computer and use it in GitHub Desktop.
Swapping An Array
public class SwappingAnArray {
public static void main(String[] args) {
int [] a = {1,2,3,4,5};
int n = a.length;
for(int i = 0; i< n/2; i++){
// Swap numbers from ith and (n-1-i)th position;
int temp = a[n-1-i];
a[n-1-i] = a[i];
a[i] = temp;
}
for( int v : a){
System.out.println(v + "");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment