Java program to swap two array.



package demo;
import java.util.Scanner;
public class SwappingArray
{
 public static void main(String[] args) 
 {
  int[] a = new int[10], b = new int[10], c = new int[10];
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter First array: ");
  for (int i = 0; i < 10; i++) {
   a[i] = sc.nextInt();
  }
  System.out.println("Enter Second array: ");
  for (int i = 0; i < 10; i++) {
   b[i] = sc.nextInt();
  }
  System.out.println("Arrays before swapping");
  System.out.println("First array: ");
  for (int i = 0; i < 10; i++) {
   System.out.print(a[i]);
  }
  System.out.println("Second array: ");
  for (int i = 0; i < 10; i++) {
   System.out.print(b[i]);
  }
  for (int i = 0; i < 10; i++) {            //write any swapping technique
   c[i] = a[i];
   a[i] = b[i];
   b[i] = c[i];
  }
  System.out.println("Arrays after swapping");
  System.out.println("First array: ");
  for (int i = 0; i < 10; i++) {
   System.out.print(a[i]);
  }
  System.out.println("Second array: ");
  for (int i = 0; i < 10; i++) {
   System.out.print(b[i]);
  }
 }
}

Output:

Enter First array: 
1
2
3
4
5
6
7
8
9
10

Enter Second array: 

11
22
3
44
55
66
77
55
22
33

Arrays before swapping


First array: 12345678910

Second array: 1122344556677552233

Arrays after swapping

First array: 1122344556677552233
Second array:  12345678910

BUILD SUCCESSFUL (total time: 27 seconds)




No comments:

Post a Comment