Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

inserting an element in a given array || java

 import java.util.*;




public class Main


{




  public static void insert (int arr[], int num, int index, int size)


  {




    for (int i = size; i > index; i--)


      {


arr[i] = arr[i - 1];


      }


    size++;


    arr[index] = num;




    //return arr;


  }

  

  public static void display(int arr[],int size)

  {

 System.out.println ("the full array :");


    for (int i = 0; i <= size; i++)


      System.out.println (arr[i]);

  }




  public static void main (String[]args)


  {


    Scanner sc = new Scanner (System.in);


    int[] arr = new int[10];


    System.out.


      printf ("Enter the number of elements should be less than 10\n");


    int size = sc.nextInt ();


    System.out.printf ("Enter the array elemetns\n");


    for (int i = 0; i < size; i++)


      {


arr[i] = sc.nextInt ();


      }


    // till here array is stored


    System.out.printf ("Enter the Index you want enter that element\n");


    int index = sc.nextInt ();


    System.out.printf ("Enter the element to be inserted\n");


    int num = sc.nextInt ();




    insert (arr, num, index, size);

    

    display(arr,size);



    /*System.out.println ("the full array :");


    for (int  = 0; i <= size; i++)


      System.out.println (arr[i]);

*/

  }


}

Post a Comment

0 Comments