Java Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 13: Write a Java program to duplicate an array entered by the user.

DuplicateArray.java

import java.util.Scanner;

public class DuplicateArray
{	
	public static void main(String[] args) 
	{
		int n;
		
		Scanner s = new Scanner(System.in);
		
		System.out.println("Enter the number of elements to be included in the array");
		n=s.nextInt();
		
		// a1 - Original Array 
		int a1[] = new int[n];
		System.out.println("Enter elements of array");
		
		// Input Original Array
		for(int i=0; i < n; i++)
		{
			System.out.print("Enter "+(i+1)+" element: ");
			a1[i]=s.nextInt();
		}
		
		int a2[]=new int[n];
		
		// Duplicating Original Array
		for(int i=0;i < n;i++)
		{
			a2[i]=a1[i];
		}
		
		System.out.println("Original Array");
		for(int i=0;i < n;i++)
		{
			System.out.print(a1[i]+" ");
		}
		
		System.out.println();
		
		System.out.println("Duplicate Array");
		for(int i=0;i < n;i++)
		{
			System.out.print(a1[i]+" ");
		}
	}
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.