DELETION IN ARRAY
PROGRAM TO DELETE AN ELEMENT FROM ARRAY
import java.util.Scanner; //Import the Scanner class
class array_delete
{
public static void main(String args[])
{
int num[] = new int[60];
int i, j, n, size;
System.out.print("Enter array size: ");
Scanner in = new Scanner(System.in);
size=in.nextInt();
//Insert value in size variable at run time
//size=in.nextInt();
System.out.println("Enter values in array:");
//Scanner in = new Scanner(System.in);
for(i=0; i<size; i++)
{
num[i]=in.nextInt();
}
System.out.print("Enter value to delete: ");
n=in.nextInt();
//Loop to delete value in array
for(i=0; i<size; i++)
{
if(n == num[i])
break;
}
//Loop to delete value of array
for(j=i; j<size; j++)
{
num[j] = num[j+1];
}
size --;
System.out.println("\nArray values after deletion");
for(i=0; i<size; i++)
{
System.out.println(num[i]);
}
}
}
- - - PROGRAM OUTPUT - - -
0 Comments