Write a program to store 6 integer numbers in array1 and 4 numbers in array2. Create a 3rd array to store the elements of both array1 and array2. (i.e. merge the 2 arrays)
Answers
Question:-
Write a program to store 6 integer numbers in array1 and 4 numbers in array2. Create a 3rd array to store the elements of both array1 and array2. (i.e. merge the 2 arrays)
Answer:-
This is a very easy Question....Here is the approach
Code:-
package Coder;
import java.util.*;
public class Array {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[]=new int[6]; //Array1
int b[]=new int[4]; //Array2
int c[]=new int[10]; //Array3
System.out.println("Enter elements of first array");
for(int i=0;i<6;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter elements of second array");
for(int i=0;i<4;i++)
{
b[i]=sc.nextInt();
}
System.out.println("Merged array:-");
for(int i=0;i<10;i++)
{
if(i<6)
c[i]=a[i];
else
c[i]=b[i-6];
}
for(int i=0;i<10;i++)
{
System.out.println(c[i]);
}
}
}
Variable Description:-
- a:- to accept 6 numbers from the user as input
- b:- to accept 4 numbers from the user as input
- c:- to print the combined(merged) array of a and b
- i:- loop variable