2. Write a program to delete an element from a given whose value is given or whose position is given in Java
import java.util.Scanner;
class DSPM2
{
public static void main(String args[])
{
Scanner mi = new Scanner(System.in);
int arr[]=new int[100];
System.out.println("Enter the no. of elements to be enter:");
int n =mi.nextInt();
int i;
System.out.println("Enter the elements:");
for(i=0;i<n;i++)
{
arr[i]=mi.nextInt();
}
System.out.println("Array entered is :");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}
System.out.println("Enter the position of element to be deleted(Index Number)");
int pos = mi.nextInt();
if(pos>n)
{System.out.println("Error!!\nValue of Out of Range!");}
else
{
--n;
for(i=pos;i<n;i++)
{
arr[i]=arr[i+1];
}
System.out.println("The Array after operation is:");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}
}
System.out.println("/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/");
}
}
OUTPUT:
class DSPM2
{
public static void main(String args[])
{
Scanner mi = new Scanner(System.in);
int arr[]=new int[100];
System.out.println("Enter the no. of elements to be enter:");
int n =mi.nextInt();
int i;
System.out.println("Enter the elements:");
for(i=0;i<n;i++)
{
arr[i]=mi.nextInt();
}
System.out.println("Array entered is :");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}
System.out.println("Enter the position of element to be deleted(Index Number)");
int pos = mi.nextInt();
if(pos>n)
{System.out.println("Error!!\nValue of Out of Range!");}
else
{
--n;
for(i=pos;i<n;i++)
{
arr[i]=arr[i+1];
}
System.out.println("The Array after operation is:");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}
}
System.out.println("/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/");
}
}
OUTPUT:
Comments
Post a Comment