1. Write a program to insert a new element at end as well as at a given position in an array in Java

import java.util.Scanner;

class DSPM1
{
public static void main(String args[])
{
Scanner mi = new Scanner(System.in);

int arr[]=new int[10];
int i;
System.out.println("Enter the no. of elements to be enter:");
int n =mi.nextInt();
System.out.println("Enter the elements:");
for(i=0;i<n;i++)
{
arr[i]=mi.nextInt();
}
System.out.println("Enter The element to be inserted");
int insert=mi.nextInt();
System.out.println("At which position(Enter the index number)");
int pos=mi.nextInt();
for(i=n;i>pos;i--)
{
arr[i]=arr[i-1];
}
arr[pos]=insert;
System.out.println("Inserted Successfully!!\nNow the new array");
for(i=0;i<n+1;i++)
{
System.out.println(arr[i]);
}
System.out.println("/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/");
}
}
OUTPUT:

Comments

Popular posts from this blog

Program to illustrate the concept of templates.

To create an html file to implement the concept of margin, padding using cascading style sheets.

To configure the IP address for a computer connected to LAN