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


This Java program prompts the user to enter the limits for two arrays, then asks for input to fill the arrays. It then creates a third array and stores all odd elements from the first two arrays into the third array. Finally, it prints out the elements stored in the third array. Here's how the program works:

  • The program starts by importing the java.util.Scanner class, which allows the user to input values from the command line.
  • The program then defines a class called OddNum_ThirdArray.
  • Inside the class, the program defines a main method, which is the entry point for the program.
  • The program creates a new Scanner object, which is used to read input from the user.
  • The program prompts the user to enter the limits for the first and second arrays using System.out.println().
  • The program reads in the values for the array limits using input.nextInt().
  • The program creates two arrays called a and b with the sizes specified by the user.
  • The program uses two for loops to fill the arrays with input values from the user.
  • The program creates a third array called c with a size of 100.
  • The program initializes a variable called k to 0, which will be used to keep track of the number of elements stored in the third array.
  • The program uses two more for loops to iterate through the first two arrays and store all odd elements in the third array.
  • For each odd element, the program assigns the value to the k-th index of the c array, then increments k.
  • The program prints out a message indicating that it is storing odd elements in the third array.
  • The program uses a for loop to print out each element in the third array.
  • The program ends.

Overall, this program is a simple example of how to create and manipulate arrays in Java. It is useful for beginners who are just learning how to use arrays and loops in Java.

Source Code

import java.util.Scanner;
class OddNum_ThirdArray
{
	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==1)
			{
				c[k]=a[i];
				k++;
			}
		}
		for(int i=0;i<m;i++)
		{
			if(b[i]%2==1)
			{
				c[k]=b[i];
				k++;
			}
		}
		System.out.print("\nOdd 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 :3
Enter the Second Array Limit :5
Element of a[0] :1
Element of a[1] :2
Element of a[2] :3
Element of b[0] :4
Element of b[1] :5
Element of b[2] :6
Element of b[3] :7
Element of b[4] :8

Odd Element Store in Third Array ...

c[0] = 1
c[1] = 3
c[2] = 5
c[3] = 7

Example Programs