KnowledgeBoat Logo
|

Computer Applications

Suppose A, B, C are arrays of integers of sizes m, n, m + n respectively. Give a program to produce a third array C, containing all the data of array A and B.

Java

Java Arrays

6 Likes

Answer

import java.util.Scanner;

public class KboatArrayMerge
{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter size m : ");
        int m = in.nextInt();
        int A[] = new int[m];
        
        System.out.print("Enter size n : ");
        int n = in.nextInt();
        int B[] = new int[n];
        
        int C[] = new int[m + n];
        
        System.out.println("Enter elements of array A:");
        for(int i = 0; i < m; i++)  {
            A[i] = in.nextInt();
        }
        
        System.out.println("Enter elements of array B:");
        for(int i = 0; i < n; i++)  {
            B[i] = in.nextInt();
        }
        
        int idx = 0;
        while(idx < A.length) {
            C[idx] = A[idx];
            idx++;
        }
        
        int j = 0;
        while(j < B.length) {
            C[idx++] = B[j++];
        }
        
        System.out.println("Elements of Array C :");
        for(int i = 0; i < C.length; i++)  {
            System.out.print(C[i] + " ");
        }  
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Suppose A, B, C are arrays of integers of sizes m, n, m + n respectively. Give a program to produce a third array C, containing all the data of array A and B.

Answered By

2 Likes


Related Questions