A Java code to add or delete other number of N number of students.
A Java code to add or delete other number of N number of students.
import java.util.*;
class r14
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of students");
int n=sc.nextInt();
int d=0,a=0;
int ar[]=new int [n+1];
System.out.println("Enter Aadhaar number for "+n+" student " );
for (int i = 0; i < n; i++)
{
ar[i] = sc.nextInt();
}
System.out.println("\nMenu:");
System.out.println("1. Add Aadhaar number");
System.out.println("2. Delete Aadhaar number");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();//to store the choice
if(choice==1)
{
System.out.println("Enter Aadhaar number to add: ");
a = sc.nextInt();
System.out.println("Input the posistion of the number to be inserted");
int pos=sc.nextInt();
for(int i=n-1;i>=pos-1;i--)//loop th shift elemnts
{
ar[i+1]=ar[i];//shifting takes place.
}
ar[pos-1]=a;//elemest is being added.
System.out.println("Updated List :");
for(int i=0;i<n+1;i++)
{
System.out.println(ar[i]);//printing new list.
}
System.out.println();
}
else if (choice == 2)
{
System.out.print("Enter Aadhaar number to delete: ");
d = sc.nextInt();
System.out.println("Input the posistion of the number to be deleted");
int pos=sc.nextInt();
for(int i=pos;i<n;i++)
{
ar[i-1]=ar[i];//element is being deleted
}
System.out.println("Updated List :");
for(int i=0;i<n-1;i++)
{
System.out.println(ar[i]);//printing new list.
}
System.out.println();
}
else
{
System.out.println("INVALID INPUT OF CHOICE");
}
}
}