48 lines
1.3 KiB
Java
48 lines
1.3 KiB
Java
class Swap {
|
|
|
|
static void swap(int a, int b) {
|
|
int temp = a;
|
|
a = b;
|
|
b = temp;
|
|
System.out.println("Inside swap method: a = " + a + ", b = " + b);
|
|
}
|
|
|
|
static void swap(int[] arr) {
|
|
int temp = arr[0];
|
|
arr[0] = arr[1];
|
|
arr[1] = temp;
|
|
System.out.println(
|
|
"Inside swap method: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
|
);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
java.util.Scanner scanner = new java.util.Scanner(System.in);
|
|
|
|
System.out.print("Enter value for x: ");
|
|
int x = scanner.nextInt();
|
|
System.out.print("Enter value for y: ");
|
|
int y = scanner.nextInt();
|
|
|
|
System.out.println("Before swap: x = " + x + ", y = " + y);
|
|
swap(x, y);
|
|
System.out.println("After swap: x = " + x + ", y = " + y);
|
|
|
|
int[] arr = new int[2];
|
|
System.out.print("\nEnter value for arr[0]: ");
|
|
arr[0] = scanner.nextInt();
|
|
System.out.print("Enter value for arr[1]: ");
|
|
arr[1] = scanner.nextInt();
|
|
|
|
System.out.println(
|
|
"Before swap: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
|
);
|
|
swap(arr);
|
|
System.out.println(
|
|
"After swap: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
|
);
|
|
|
|
scanner.close();
|
|
}
|
|
}
|