Write a program that identifies the Even elements in two arrays and creates a third array


This is a Java program that takes two integer arrays as input from the user and creates a third array containing only the even numbers from the first two arrays. Here is a brief explanation of the code:

  • The program imports the Scanner class from the java.util package to read input from the user.
  • The program defines an integer array c to store the even numbers from the first two arrays.
  • The program prompts the user to enter the sizes of the two arrays and creates two arrays of those sizes.
  • The program then prompts the user to enter the elements of the two arrays and stores them in the arrays using for loops.
  • The program uses two more for loops to iterate through each element of the two arrays and store the even numbers in the third array.
  • The program prints the third array to the console, which contains only the even numbers from the first two arrays.

Source Code

import java.util.Scanner;
class EvenNum_ThridArray
{
	public static void main(String[] args)
	{   
		Scanner input =new Scanner(System.in);
		System.out.print("Enter the First Array Limit :");
		int n =input.nextInt();
		System.out.print("Enter the Second Array Limit :");
		int m =input.nextInt();
		int [] a =new int[n];
		int [] b =new int[m];
		int [] c =new int[100];
		int k=0;
        for(int i=0;i<n;i++)
        {
            System.out.printf("Element of a[%d] :",i);
            a[i]=input.nextInt();
        }
        for(int i=0;i<m;i++)
        {
            System.out.printf("Element of b[%d] :",i);
            b[i]=input.nextInt();
        }
		for(int i=0;i<n;i++)
		{
			if(a[i]%2==0)
			{
				c[k]=a[i];
				k++;
			}
		}
		for(int i=0;i<m;i++)
		{
			if(b[i]%2==0)
			{
				c[k]=b[i];
				k++;
			}
		}
		System.out.print("\nEven Element Store in Third Array ...\n");	
		for(int i=0;i<k;i++)
		{
			System.out.printf("\nc[%d] = %d",i,c[i]);
		}
    }
}
 

Output

Enter the First Array Limit :5
Enter the Second Array Limit :3
Element of a[0] :1
Element of a[1] :2
Element of a[2] :3
Element of a[3] :4
Element of a[4] :5
Element of b[0] :6
Element of b[1] :7
Element of b[2] :8

Even Element Store in Third Array ...

c[0] = 2
c[1] = 4
c[2] = 6
c[3] = 8

Example Programs