← All resources

Write a program to input 2 arrays A and B of size M and N and merge them into a third array of size M+N

import java.io.*;
public class Merge_Array
{
    public static void main()throws IOException
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int s1, s2;
        int a[], b[], c[], i;
        System.out.println("Enter the size of the first array:");
        s1 = Integer.parseInt(r.readLine());
        s2 = Integer.parseInt(r.readLine());
        a = new int[s1];
        b = new int[s2];
        c = new int[s1+s2];
        System.out.println("Enter the first array:");
        for(i=0;i< s1;i++)
        {
            a[i] = Integer.parseInt(r.readLine());
        }
        System.out.println("Enter the second array:");
        for(i=0;i< s2;i++)
        {
            b[i] = Integer.parseInt(r.readLine());
        }
        for(i=0;i< s1+s2;i++)
        {
            if(i< s1)
            c[i] = a[i];
            else
            c[i] = b[i-s1];
        }
        System.out.println("The merged array is:");
        for(i=0;i< s1+s2;i++)
        {
            System.out.println(c[i]);
        }
    }
}