Write a program to make suitable use of Scanner Class and its functions to input N integers in two single dimensional arrays X[] and Y[]. create another array z[] that contains integers from arrays X[] and Y[] that are common in both. Print All Three Arrays.
Solution: First of all declare Three Array. Populate first two array. now pick every elements of first array and search it in the second array. If any elements of first array found in the second array then put it in the third array. Now print the all three array.
import java.util.*; public class Common { public static void main() { Scanner sc = new Scanner(System.in); System.out.println("enter 5 integers for the 1st array:"); int x[] = new int[5]; int i,k,j,p; for( i = 0 ; i<5 ; i++) { x[i]=sc.nextInt(); } System.out.println("enter 5 integers for the 2nd array:"); int y[] = new int[5]; for( k = 0 ; k<5 ; k++) { y[k]=sc.nextInt(); } int z[] = new int[10]; k=0; for(j=0 ; j<5 ; j++) { for(p=0 ; p<5 ; p++) { if(x[j]==y[p]) { z[k++]=x[j]; } } } System.out.println("X[] Array : "); for(j=0 ; j<5 ; j++) { System.out.print(x[j] + " "); } System.out.println("\nY[] Array : "); for(j=0 ; j<5 ; j++) { System.out.print(y[j] + " "); } System.out.println("\nZ[] Array : "); for(p=0;pOutput
enter 5 integers for the 1st array:
12
23
34
45
56
enter 5 integers for the 2nd array:
21
23
434
54
56
X[] Array :
12 23 34 45 56
Y[] Array :
21 23 434 54 56
Z[] Array :
23 56